
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using SJZip;
using Spacejock;
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using yW5Lib.My;

namespace yW5Lib
{
    public class clsProject
    {
        public delegate void ProgressEventHandler(string ProgressString);

        public delegate void AddedNewSceneEventHandler(int ID);

        public delegate void AddedNewCharEventHandler(int ID);

        public delegate void AddedNewChapterEventHandler(int ID);

        public delegate void AddedNewItemEventHandler(int ID);

        public delegate void AddedNewLocationEventHandler(int ID);

        public delegate void RemovedSceneEventHandler(int ID);

        public delegate void RemovedCharEventHandler(int ID);

        public delegate void RemovedChapterEventHandler(int ID);

        public delegate void RemovedItemEventHandler(int ID);

        public delegate void RemovedLocationEventHandler(int ID);

        public delegate void HideSplashEventHandler();

        public delegate void SavedNoteEventHandler(int ID);

        public delegate void SavedSceneEventHandler(int ID);

        public delegate void SavedCharEventHandler(int ID);

        public delegate void SavedChapterEventHandler(int ID);

        public delegate void SavedItemEventHandler(int ID);

        public delegate void SavedLocationEventHandler(int ID);

        private class clsPair
        {
            public int OldID;

            public int NewID;
        }

        private clsXml cXml;

        [CompilerGenerated] [AccessedThroughProperty("cProjectIO")]
        private clsProjectIO _cProjectIO;

        private Collection colSaveTokens;

        [CompilerGenerated] [AccessedThroughProperty("SaveTimer")]
        private System.Windows.Forms.Timer _SaveTimer;

        private string strSaveToken;

        [CompilerGenerated] private ProgressEventHandler ProgressEvent;

        public clsVars cVars;

        public clsExports cExports;

        [CompilerGenerated] private AddedNewSceneEventHandler AddedNewSceneEvent;

        [CompilerGenerated] private AddedNewCharEventHandler AddedNewCharEvent;

        [CompilerGenerated] private AddedNewChapterEventHandler AddedNewChapterEvent;

        [CompilerGenerated] private AddedNewItemEventHandler AddedNewItemEvent;

        [CompilerGenerated] private AddedNewLocationEventHandler AddedNewLocationEvent;

        [CompilerGenerated] private RemovedSceneEventHandler RemovedSceneEvent;

        [CompilerGenerated] private RemovedCharEventHandler RemovedCharEvent;

        [CompilerGenerated] private RemovedChapterEventHandler RemovedChapterEvent;

        [CompilerGenerated] private RemovedItemEventHandler RemovedItemEvent;

        [CompilerGenerated] private RemovedLocationEventHandler RemovedLocationEvent;

        [CompilerGenerated] private HideSplashEventHandler HideSplashEvent;

        [CompilerGenerated] private SavedNoteEventHandler SavedNoteEvent;

        [CompilerGenerated] private SavedSceneEventHandler SavedSceneEvent;

        [CompilerGenerated] private SavedCharEventHandler SavedCharEvent;

        [CompilerGenerated] private SavedChapterEventHandler SavedChapterEvent;

        [CompilerGenerated] private SavedItemEventHandler SavedItemEvent;

        [CompilerGenerated] private SavedLocationEventHandler SavedLocationEvent;

        [CompilerGenerated] [AccessedThroughProperty("cChapters")]
        private clsChapters _cChapters;

        [CompilerGenerated] [AccessedThroughProperty("cLocations")]
        private clsLocations _cLocations;

        [CompilerGenerated] [AccessedThroughProperty("cItems")]
        private clsItems _cItems;

        [CompilerGenerated] [AccessedThroughProperty("cChars")]
        private clsCharacters _cChars;

        [CompilerGenerated] [AccessedThroughProperty("cNotes")]
        private clsNotes _cNotes;

        [CompilerGenerated] [AccessedThroughProperty("cScenes")]
        private clsScenes _cScenes;

        private clsBooks cBooks;

        private clsParts cParts;

        private string mvarXMLFilename;

        private const int SystemXMLFileVersion = 4;

        private int mvarXMLFileVersion;

        private bool mvarV6;

        private string mvarTITLE;

        private string mvarBio;

        private string mvarAuthorname;

        private string mvarDesc;

        private string mvarLatexHeaderFile;

        private DateTime mvarWordTargetStartDate;

        private DateTime mvarWordTargetEndDate;

        private int mvarWordTarget;

        private bool mvarLatexSingleQuotes;

        private bool mvarLatexSuppressChapterTitles;

        private int mvarLatexSceneDividerStyle;

        private string mvarHtmlSceneDivider;

        private string mvarHTMLEncoding;

        private bool mvarHTMLUnicode;

        private int mvarEndFrontMatterChID;

        private int mvarEndMiddleChID;

        private int mvarEndBeginningChID;

        private int mvarWordCountStart;

        private DateTime[] mvarDeadlineDate;

        private DateTime[] mvarDeadlineStartDate;

        private string[] mvarFieldTitle;

        private string mvarPassword;

        private bool mvarUsePassword;

        private bool mvarCollab;

        private string mvarCollabFolder;

        private int mvarLastViewedScID;

        private DateTime mvarLastFullBackup;

        private string mvarLastFullBackupFilename;

        private DateTime mvarMRUSpecificDate;

        private DateTime mvarMRUSpecificTime;

        private clsWordLogs mvarWordLogs;

        private bool mvarSaving;

        private bool mvarLoading;
        [SpecialName] private bool AlreadyCalculating;

        protected virtual clsProjectIO cProjectIO
        {
            [CompilerGenerated] get { return _cProjectIO; }
            [MethodImpl(MethodImplOptions.Synchronized)]
            [CompilerGenerated]
            set
            {
                clsProjectIO.ProgressEventHandler obj = ProgressHandler;
                clsProjectIO cProjectIO = _cProjectIO;
                if (cProjectIO != null)
                {
                    cProjectIO.Progress -= obj;
                }

                _cProjectIO = value;
                cProjectIO = _cProjectIO;
                if (cProjectIO != null)
                {
                    cProjectIO.Progress += obj;
                }
            }
        }

        protected virtual System.Windows.Forms.Timer SaveTimer
        {
            [CompilerGenerated] get { return _SaveTimer; }
            [MethodImpl(MethodImplOptions.Synchronized)]
            [CompilerGenerated]
            set
            {
                EventHandler value2 = SaveTimer_Tick;
                System.Windows.Forms.Timer saveTimer = _SaveTimer;
                if (saveTimer != null)
                {
                    saveTimer.Tick -= value2;
                }

                _SaveTimer = value;
                saveTimer = _SaveTimer;
                if (saveTimer != null)
                {
                    saveTimer.Tick += value2;
                }
            }
        }

        protected virtual clsChapters cChapters
        {
            [CompilerGenerated] get { return _cChapters; }
            [MethodImpl(MethodImplOptions.Synchronized)]
            [CompilerGenerated]
            set
            {
                clsChapters.SavedEventHandler obj = cChapters_Saved;
                clsChapters cChapters = _cChapters;
                if (cChapters != null)
                {
                    cChapters.Saved -= obj;
                }

                _cChapters = value;
                cChapters = _cChapters;
                if (cChapters != null)
                {
                    cChapters.Saved += obj;
                }
            }
        }

        protected virtual clsLocations cLocations
        {
            [CompilerGenerated] get { return _cLocations; }
            [MethodImpl(MethodImplOptions.Synchronized)]
            [CompilerGenerated]
            set
            {
                clsLocations.SavedEventHandler obj = cLocations_Saved;
                clsLocations cLocations = _cLocations;
                if (cLocations != null)
                {
                    cLocations.Saved -= obj;
                }

                _cLocations = value;
                cLocations = _cLocations;
                if (cLocations != null)
                {
                    cLocations.Saved += obj;
                }
            }
        }

        protected virtual clsItems cItems
        {
            [CompilerGenerated] get { return _cItems; }
            [MethodImpl(MethodImplOptions.Synchronized)]
            [CompilerGenerated]
            set
            {
                clsItems.SavedEventHandler obj = cItems_Saved;
                clsItems cItems = _cItems;
                if (cItems != null)
                {
                    cItems.Saved -= obj;
                }

                _cItems = value;
                cItems = _cItems;
                if (cItems != null)
                {
                    cItems.Saved += obj;
                }
            }
        }

        protected virtual clsCharacters cChars
        {
            [CompilerGenerated] get { return _cChars; }
            [MethodImpl(MethodImplOptions.Synchronized)]
            [CompilerGenerated]
            set
            {
                clsCharacters.SavedEventHandler obj = cChars_Saved;
                clsCharacters cChars = _cChars;
                if (cChars != null)
                {
                    cChars.Saved -= obj;
                }

                _cChars = value;
                cChars = _cChars;
                if (cChars != null)
                {
                    cChars.Saved += obj;
                }
            }
        }

        protected virtual clsNotes cNotes
        {
            [CompilerGenerated] get { return _cNotes; }
            [MethodImpl(MethodImplOptions.Synchronized)]
            [CompilerGenerated]
            set
            {
                clsNotes.SavedEventHandler obj = cNotes_SavedNote;
                clsNotes cNotes = _cNotes;
                if (cNotes != null)
                {
                    cNotes.Saved -= obj;
                }

                _cNotes = value;
                cNotes = _cNotes;
                if (cNotes != null)
                {
                    cNotes.Saved += obj;
                }
            }
        }

        protected virtual clsScenes cScenes
        {
            [CompilerGenerated] get { return _cScenes; }
            [MethodImpl(MethodImplOptions.Synchronized)]
            [CompilerGenerated]
            set
            {
                clsScenes.SavedEventHandler obj = cScenes_SavedScene;
                clsScenes.LoadingNumEventHandler obj2 = cScenes_LoadingNum;
                clsScenes.SavingNumEventHandler obj3 = cScenes_SavingNum;
                clsScenes cScenes = _cScenes;
                if (cScenes != null)
                {
                    cScenes.Saved -= obj;
                    cScenes.LoadingNum -= obj2;
                    cScenes.SavingNum -= obj3;
                }

                _cScenes = value;
                cScenes = _cScenes;
                if (cScenes != null)
                {
                    cScenes.Saved += obj;
                    cScenes.LoadingNum += obj2;
                    cScenes.SavingNum += obj3;
                }
            }
        }

        public bool Saving => mvarSaving;

        public bool Loading => mvarLoading;

        public clsWordLogs WordLogs => mvarWordLogs;

        public DateTime LastFullBackup
        {
            get { return mvarLastFullBackup; }
            set { mvarLastFullBackup = value; }
        }

        public string LastFullBackupFilename
        {
            get { return mvarLastFullBackupFilename; }
            set { mvarLastFullBackupFilename = value; }
        }

        public bool V6File
        {
            get { return mvarV6; }
            set { mvarV6 = value; }
        }

        public clsBooks Books => cBooks;

        public clsParts Parts => cParts;

        public clsScenes Scenes => cScenes;

        public clsChapters Chapters => cChapters;

        public clsLocations Locations => cLocations;

        public clsItems Items => cItems;

        public clsCharacters Chars => cChars;

        public clsNotes Notes => cNotes;

        public DateTime WordTargetStartDate
        {
            get { return mvarWordTargetStartDate; }
            set { mvarWordTargetStartDate = value; }
        }

        public int LastViewedScID
        {
            get { return mvarLastViewedScID; }
            set { mvarLastViewedScID = value; }
        }

        public bool UsePassword
        {
            get { return mvarUsePassword; }
            set { mvarUsePassword = value; }
        }

        public bool Collab
        {
            get { return mvarCollab; }
            set { mvarCollab = value; }
        }

        public string CollabFolder
        {
            get { return mvarCollabFolder; }
            set { mvarCollabFolder = value; }
        }

        public bool LatexSingleQuotes
        {
            get { return mvarLatexSingleQuotes; }
            set { mvarLatexSingleQuotes = value; }
        }

        public bool LatexSuppressChapterTitles
        {
            get { return mvarLatexSuppressChapterTitles; }
            set { mvarLatexSuppressChapterTitles = value; }
        }

        public int LatexSceneDividerStyle
        {
            get { return mvarLatexSceneDividerStyle; }
            set { mvarLatexSceneDividerStyle = value; }
        }

        public string HtmlSceneDivider
        {
            get { return mvarHtmlSceneDivider; }
            set { mvarHtmlSceneDivider = value; }
        }

        public string HtmlEncoding
        {
            get { return mvarHTMLEncoding; }
            set { mvarHTMLEncoding = value; }
        }

        public bool HtmlUnicode
        {
            get { return mvarHTMLUnicode; }
            set { mvarHTMLUnicode = value; }
        }

        public DateTime WordTargetEndDate
        {
            get { return mvarWordTargetEndDate; }
            set { mvarWordTargetEndDate = value; }
        }

        public int WordTarget
        {
            get { return mvarWordTarget; }
            set { mvarWordTarget = value; }
        }

        public int WordCountStart
        {
            get { return mvarWordCountStart; }
            set { mvarWordCountStart = value; }
        }

        public DateTime MRUSpecificDate
        {
            get { return mvarMRUSpecificDate; }
            set { mvarMRUSpecificDate = value; }
        }

        public DateTime MRUSpecificTime
        {
            get { return mvarMRUSpecificTime; }
            set { mvarMRUSpecificTime = value; }
        }

        public DateTime get_DeadlineDate(int Index)
        {
            DateTime dateTime = new DateTime();
            try
            {
                dateTime = this.mvarDeadlineDate[Index];
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
            }

            return dateTime;
        }

        public void set_DeadlineDate(int Index, DateTime Value)
        {
            try
            {
                this.mvarDeadlineDate[Index] = Value;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
            }
        }

        public DateTime get_DeadlineStartDate(int Index)
        {
            DateTime dateTime = new DateTime();
            try
            {
                dateTime = this.mvarDeadlineStartDate[Index];
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
            }

            return dateTime;
        }

        public void set_DeadlineStartDate(int Index, DateTime Value)
        {
            try
            {
                this.mvarDeadlineStartDate[Index] = Value;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
            }
        }

        public string get_FieldTitle(int Index)
        {
            string translation = null;
            try
            {
                translation = this.mvarFieldTitle[Index];
                if (Operators.CompareString(translation, "", false) == 0)
                    translation = clsShared.cLang.GetTranslation(modConst.FieldDefault(Index));
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
            }

            return translation;
        }

        public void set_FieldTitle(int Index, string Value)
        {
            try
            {
                this.mvarFieldTitle[Index] = Value;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                ProjectData.ClearProjectError();
            }
        }

        public string Title
        {
            get { return mvarTITLE; }
            set { mvarTITLE = value; }
        }

        public string AuthorName
        {
            get { return mvarAuthorname; }
            set { mvarAuthorname = value; }
        }

        public string Password
        {
            get { return mvarPassword; }
            set { mvarPassword = value; }
        }

        public int EndFrontmatterChID
        {
            get { return mvarEndFrontMatterChID; }
            set { mvarEndFrontMatterChID = value; }
        }

        public int EndBeginningChID
        {
            get { return mvarEndBeginningChID; }
            set { mvarEndBeginningChID = value; }
        }

        public int EndMiddleChID
        {
            get { return mvarEndMiddleChID; }
            set { mvarEndMiddleChID = value; }
        }

        public int get_ChapterSceneCount(int ChID, bool WithUnused)
        {
            clsChapter clsChapter = this.get_ChapterByID(ChID);
            int num1;
            if (WithUnused)
            {
                num1 = clsChapter.SceneCount;
            }
            else
            {
                int num2 = 0;
                int sceneCount = clsChapter.SceneCount;
                for (int Index = 1; Index <= sceneCount; ++Index)
                {
                    if (!this.get_SceneByID(clsChapter.get_SceneIDByIndex(Index)).Unused)
                        ++num2;
                }

                num1 = num2;
            }

            return num1;
        }

        public int get_ChapterWordcount(int ChID, bool WithUnused)
        {
            clsChapter clsChapter = this.get_ChapterByID(ChID);
            int num = 0;
            int sceneCount = clsChapter.SceneCount;
            for (int IDX = 1; IDX <= sceneCount; ++IDX)
            {
                clsScene clsScene = this.get_SceneByChID(ChID, IDX);
                if (WithUnused)
                    num += clsScene.Wordcount;
                else if (!clsChapter.Unused && clsChapter.Type == ChapterTypes.ChapterNormal && !clsScene.Unused)
                    num += clsScene.Wordcount;
            }

            return num;
        }

        public int get_ChapterWordcount(clsChapter Chapter, bool WithUnused)
        {
            int num = 0;
            int sceneCount = Chapter.SceneCount;
            for (int Index = 1; Index <= sceneCount; ++Index)
            {
                clsScene clsScene = this.get_SceneByID(Chapter.get_SceneIDByIndex(Index));
                if (WithUnused)
                    num += clsScene.Wordcount;
                else if (!Chapter.Unused && Chapter.Type == ChapterTypes.ChapterNormal && !clsScene.Unused)
                    num += clsScene.Wordcount;
            }

            return num;
        }

        public long get_ChapterLetterCount(int ChID)
        {
            clsChapter clsChapter = this.get_ChapterByID(ChID);
            long num = 0;
            int sceneCount = clsChapter.SceneCount;
            for (int IDX = 1; IDX <= sceneCount; ++IDX)
            {
                clsScene clsScene = this.get_SceneByChID(ChID, IDX);
                num += (long) clsScene.Lettercount;
            }

            return num;
        }


        public int ChapterCount => cChapters.Count;

        public int SceneCount => cScenes.Count;

        public int UsedSceneCount
        {
            get
            {
                int num = 0;
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = Scenes.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsScene clsScene = (clsScene) enumerator.Current;
                        if (!(clsScene.Unused | this.get_ChapterByID(clsScene.BelongsToChID).Unused |
                              (this.get_ChapterByID(clsScene.BelongsToChID).Type == ChapterTypes.ChapterNotes)))
                        {
                            num++;
                        }
                    }

                    return num;
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
        }

        public int LocationCount => cLocations.Count;

        public int ItemCount => cItems.Count;

        public int NoteCount => cNotes.Count;

        private clsChapter get_ChapterByIndex(int Index)
        {
            return this.cChapters.ChapterByIndex(Index);
        }

        public clsChapter get_ChapterByID(int ID)
        {
            return this.cChapters.ChapterByID(ID);
        }

        public clsLocation get_LocationByIndex(int Index)
        {
            return this.cLocations.LocationByIndex(Index);
        }

        public clsLocation get_LocationByID(int ID)
        {
            return this.cLocations.LocationByID(ID);
        }

        private clsItem get_ItemByIndex(int Index)
        {
            return this.cItems.ItemByIndex(Index);
        }

        public clsItem get_ItemByID(int ID)
        {
            return this.cItems.ItemByID(ID);
        }

        private clsScene get_SceneByIndex(int Index)
        {
            return this.cScenes.SceneByIndex(Index);
        }

        public clsScene get_SceneByID(int ID)
        {
            return this.cScenes.SceneByID(ID);
        }

        public clsScene get_SceneByChID(int ChID, int IDX)
        {
            return this.get_SceneByID(this.get_ChapterByID(ChID).get_SceneIDByIndex(IDX));
        }

        public clsNote get_NoteByID(int ID)
        {
            return this.cNotes.NoteByID(ID);
        }

        public clsCharacter get_CharByID(int ID)
        {
            clsCharacter clsCharacter;
            if (ID == 0)
            {
                clsCharacter = new clsCharacter();
                clsCharacter.Title = "N/A";
            }
            else
                clsCharacter = this.cChars.CharByID(ID);

            return clsCharacter;
        }

        public int CharCount
        {
            get { return this.cChars.Count; }
        }

        private clsCharacter get_CharByIndex(int Index)
        {
            return this.cChars.CharByIndex(Index);
        }

        private clsBook get_BookByIndex(int Index)
        {
            return this.cBooks.BookByIndex(Index);
        }

        public clsBook get_BookByID(int ID)
        {
            return this.cBooks.BookByID(ID);
        }

        private clsPart get_PartByIndex(int Index)
        {
            return this.cParts.PartByIndex(Index);
        }

        public clsPart get_PartByID(int ID)
        {
            return this.cParts.PartByID(ID);
        }

        public bool IsEmpty
        {
            get
            {
                if (Operators.CompareString(FileWithPath, "", TextCompare: false) == 0)
                {
                    return true;
                }

                return false;
            }
        }

        public string FileWithPath
        {
            get { return mvarXMLFilename; }
            set { mvarXMLFilename = value; }
        }

        public string DescText
        {
            get { return mvarDesc; }
            set { mvarDesc = value; }
        }

        public string LatexHeaderFile
        {
            get { return mvarLatexHeaderFile; }
            set { mvarLatexHeaderFile = value; }
        }

        public string AuthorBio
        {
            get { return mvarBio; }
            set { mvarBio = value; }
        }

        public string LogsFolder => Path.Combine(ProjectPath(), "Logs");

        public string ImagesFolder => Path.Combine(ProjectPath(), "Images");

        public string AutobackupsFolder => Path.Combine(ProjectPath(), "Autobackups");

        public event ProgressEventHandler Progress
        {
            [CompilerGenerated]
            add
            {
                ProgressEventHandler progressEventHandler = ProgressEvent;
                ProgressEventHandler progressEventHandler2;
                do
                {
                    progressEventHandler2 = progressEventHandler;
                    ProgressEventHandler value2 = (ProgressEventHandler) Delegate.Combine(progressEventHandler2, value);
                    progressEventHandler =
                        Interlocked.CompareExchange(ref ProgressEvent, value2, progressEventHandler2);
                } while ((object) progressEventHandler != progressEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                ProgressEventHandler progressEventHandler = ProgressEvent;
                ProgressEventHandler progressEventHandler2;
                do
                {
                    progressEventHandler2 = progressEventHandler;
                    ProgressEventHandler value2 = (ProgressEventHandler) Delegate.Remove(progressEventHandler2, value);
                    progressEventHandler =
                        Interlocked.CompareExchange(ref ProgressEvent, value2, progressEventHandler2);
                } while ((object) progressEventHandler != progressEventHandler2);
            }
        }

        public event AddedNewSceneEventHandler AddedNewScene
        {
            [CompilerGenerated]
            add
            {
                AddedNewSceneEventHandler addedNewSceneEventHandler = AddedNewSceneEvent;
                AddedNewSceneEventHandler addedNewSceneEventHandler2;
                do
                {
                    addedNewSceneEventHandler2 = addedNewSceneEventHandler;
                    AddedNewSceneEventHandler value2 =
                        (AddedNewSceneEventHandler) Delegate.Combine(addedNewSceneEventHandler2, value);
                    addedNewSceneEventHandler =
                        Interlocked.CompareExchange(ref AddedNewSceneEvent, value2, addedNewSceneEventHandler2);
                } while ((object) addedNewSceneEventHandler != addedNewSceneEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                AddedNewSceneEventHandler addedNewSceneEventHandler = AddedNewSceneEvent;
                AddedNewSceneEventHandler addedNewSceneEventHandler2;
                do
                {
                    addedNewSceneEventHandler2 = addedNewSceneEventHandler;
                    AddedNewSceneEventHandler value2 =
                        (AddedNewSceneEventHandler) Delegate.Remove(addedNewSceneEventHandler2, value);
                    addedNewSceneEventHandler =
                        Interlocked.CompareExchange(ref AddedNewSceneEvent, value2, addedNewSceneEventHandler2);
                } while ((object) addedNewSceneEventHandler != addedNewSceneEventHandler2);
            }
        }

        public event AddedNewCharEventHandler AddedNewChar
        {
            [CompilerGenerated]
            add
            {
                AddedNewCharEventHandler addedNewCharEventHandler = AddedNewCharEvent;
                AddedNewCharEventHandler addedNewCharEventHandler2;
                do
                {
                    addedNewCharEventHandler2 = addedNewCharEventHandler;
                    AddedNewCharEventHandler value2 =
                        (AddedNewCharEventHandler) Delegate.Combine(addedNewCharEventHandler2, value);
                    addedNewCharEventHandler =
                        Interlocked.CompareExchange(ref AddedNewCharEvent, value2, addedNewCharEventHandler2);
                } while ((object) addedNewCharEventHandler != addedNewCharEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                AddedNewCharEventHandler addedNewCharEventHandler = AddedNewCharEvent;
                AddedNewCharEventHandler addedNewCharEventHandler2;
                do
                {
                    addedNewCharEventHandler2 = addedNewCharEventHandler;
                    AddedNewCharEventHandler value2 =
                        (AddedNewCharEventHandler) Delegate.Remove(addedNewCharEventHandler2, value);
                    addedNewCharEventHandler =
                        Interlocked.CompareExchange(ref AddedNewCharEvent, value2, addedNewCharEventHandler2);
                } while ((object) addedNewCharEventHandler != addedNewCharEventHandler2);
            }
        }

        public event AddedNewChapterEventHandler AddedNewChapter
        {
            [CompilerGenerated]
            add
            {
                AddedNewChapterEventHandler addedNewChapterEventHandler = AddedNewChapterEvent;
                AddedNewChapterEventHandler addedNewChapterEventHandler2;
                do
                {
                    addedNewChapterEventHandler2 = addedNewChapterEventHandler;
                    AddedNewChapterEventHandler value2 =
                        (AddedNewChapterEventHandler) Delegate.Combine(addedNewChapterEventHandler2, value);
                    addedNewChapterEventHandler = Interlocked.CompareExchange(ref AddedNewChapterEvent, value2,
                        addedNewChapterEventHandler2);
                } while ((object) addedNewChapterEventHandler != addedNewChapterEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                AddedNewChapterEventHandler addedNewChapterEventHandler = AddedNewChapterEvent;
                AddedNewChapterEventHandler addedNewChapterEventHandler2;
                do
                {
                    addedNewChapterEventHandler2 = addedNewChapterEventHandler;
                    AddedNewChapterEventHandler value2 =
                        (AddedNewChapterEventHandler) Delegate.Remove(addedNewChapterEventHandler2, value);
                    addedNewChapterEventHandler = Interlocked.CompareExchange(ref AddedNewChapterEvent, value2,
                        addedNewChapterEventHandler2);
                } while ((object) addedNewChapterEventHandler != addedNewChapterEventHandler2);
            }
        }

        public event AddedNewItemEventHandler AddedNewItem
        {
            [CompilerGenerated]
            add
            {
                AddedNewItemEventHandler addedNewItemEventHandler = AddedNewItemEvent;
                AddedNewItemEventHandler addedNewItemEventHandler2;
                do
                {
                    addedNewItemEventHandler2 = addedNewItemEventHandler;
                    AddedNewItemEventHandler value2 =
                        (AddedNewItemEventHandler) Delegate.Combine(addedNewItemEventHandler2, value);
                    addedNewItemEventHandler =
                        Interlocked.CompareExchange(ref AddedNewItemEvent, value2, addedNewItemEventHandler2);
                } while ((object) addedNewItemEventHandler != addedNewItemEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                AddedNewItemEventHandler addedNewItemEventHandler = AddedNewItemEvent;
                AddedNewItemEventHandler addedNewItemEventHandler2;
                do
                {
                    addedNewItemEventHandler2 = addedNewItemEventHandler;
                    AddedNewItemEventHandler value2 =
                        (AddedNewItemEventHandler) Delegate.Remove(addedNewItemEventHandler2, value);
                    addedNewItemEventHandler =
                        Interlocked.CompareExchange(ref AddedNewItemEvent, value2, addedNewItemEventHandler2);
                } while ((object) addedNewItemEventHandler != addedNewItemEventHandler2);
            }
        }

        public event AddedNewLocationEventHandler AddedNewLocation
        {
            [CompilerGenerated]
            add
            {
                AddedNewLocationEventHandler addedNewLocationEventHandler = AddedNewLocationEvent;
                AddedNewLocationEventHandler addedNewLocationEventHandler2;
                do
                {
                    addedNewLocationEventHandler2 = addedNewLocationEventHandler;
                    AddedNewLocationEventHandler value2 =
                        (AddedNewLocationEventHandler) Delegate.Combine(addedNewLocationEventHandler2, value);
                    addedNewLocationEventHandler = Interlocked.CompareExchange(ref AddedNewLocationEvent, value2,
                        addedNewLocationEventHandler2);
                } while ((object) addedNewLocationEventHandler != addedNewLocationEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                AddedNewLocationEventHandler addedNewLocationEventHandler = AddedNewLocationEvent;
                AddedNewLocationEventHandler addedNewLocationEventHandler2;
                do
                {
                    addedNewLocationEventHandler2 = addedNewLocationEventHandler;
                    AddedNewLocationEventHandler value2 =
                        (AddedNewLocationEventHandler) Delegate.Remove(addedNewLocationEventHandler2, value);
                    addedNewLocationEventHandler = Interlocked.CompareExchange(ref AddedNewLocationEvent, value2,
                        addedNewLocationEventHandler2);
                } while ((object) addedNewLocationEventHandler != addedNewLocationEventHandler2);
            }
        }

        public event RemovedSceneEventHandler RemovedScene
        {
            [CompilerGenerated]
            add
            {
                RemovedSceneEventHandler removedSceneEventHandler = RemovedSceneEvent;
                RemovedSceneEventHandler removedSceneEventHandler2;
                do
                {
                    removedSceneEventHandler2 = removedSceneEventHandler;
                    RemovedSceneEventHandler value2 =
                        (RemovedSceneEventHandler) Delegate.Combine(removedSceneEventHandler2, value);
                    removedSceneEventHandler =
                        Interlocked.CompareExchange(ref RemovedSceneEvent, value2, removedSceneEventHandler2);
                } while ((object) removedSceneEventHandler != removedSceneEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                RemovedSceneEventHandler removedSceneEventHandler = RemovedSceneEvent;
                RemovedSceneEventHandler removedSceneEventHandler2;
                do
                {
                    removedSceneEventHandler2 = removedSceneEventHandler;
                    RemovedSceneEventHandler value2 =
                        (RemovedSceneEventHandler) Delegate.Remove(removedSceneEventHandler2, value);
                    removedSceneEventHandler =
                        Interlocked.CompareExchange(ref RemovedSceneEvent, value2, removedSceneEventHandler2);
                } while ((object) removedSceneEventHandler != removedSceneEventHandler2);
            }
        }

        public event RemovedCharEventHandler RemovedChar
        {
            [CompilerGenerated]
            add
            {
                RemovedCharEventHandler removedCharEventHandler = RemovedCharEvent;
                RemovedCharEventHandler removedCharEventHandler2;
                do
                {
                    removedCharEventHandler2 = removedCharEventHandler;
                    RemovedCharEventHandler value2 =
                        (RemovedCharEventHandler) Delegate.Combine(removedCharEventHandler2, value);
                    removedCharEventHandler =
                        Interlocked.CompareExchange(ref RemovedCharEvent, value2, removedCharEventHandler2);
                } while ((object) removedCharEventHandler != removedCharEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                RemovedCharEventHandler removedCharEventHandler = RemovedCharEvent;
                RemovedCharEventHandler removedCharEventHandler2;
                do
                {
                    removedCharEventHandler2 = removedCharEventHandler;
                    RemovedCharEventHandler value2 =
                        (RemovedCharEventHandler) Delegate.Remove(removedCharEventHandler2, value);
                    removedCharEventHandler =
                        Interlocked.CompareExchange(ref RemovedCharEvent, value2, removedCharEventHandler2);
                } while ((object) removedCharEventHandler != removedCharEventHandler2);
            }
        }

        public event RemovedChapterEventHandler RemovedChapter
        {
            [CompilerGenerated]
            add
            {
                RemovedChapterEventHandler removedChapterEventHandler = RemovedChapterEvent;
                RemovedChapterEventHandler removedChapterEventHandler2;
                do
                {
                    removedChapterEventHandler2 = removedChapterEventHandler;
                    RemovedChapterEventHandler value2 =
                        (RemovedChapterEventHandler) Delegate.Combine(removedChapterEventHandler2, value);
                    removedChapterEventHandler =
                        Interlocked.CompareExchange(ref RemovedChapterEvent, value2, removedChapterEventHandler2);
                } while ((object) removedChapterEventHandler != removedChapterEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                RemovedChapterEventHandler removedChapterEventHandler = RemovedChapterEvent;
                RemovedChapterEventHandler removedChapterEventHandler2;
                do
                {
                    removedChapterEventHandler2 = removedChapterEventHandler;
                    RemovedChapterEventHandler value2 =
                        (RemovedChapterEventHandler) Delegate.Remove(removedChapterEventHandler2, value);
                    removedChapterEventHandler =
                        Interlocked.CompareExchange(ref RemovedChapterEvent, value2, removedChapterEventHandler2);
                } while ((object) removedChapterEventHandler != removedChapterEventHandler2);
            }
        }

        public event RemovedItemEventHandler RemovedItem
        {
            [CompilerGenerated]
            add
            {
                RemovedItemEventHandler removedItemEventHandler = RemovedItemEvent;
                RemovedItemEventHandler removedItemEventHandler2;
                do
                {
                    removedItemEventHandler2 = removedItemEventHandler;
                    RemovedItemEventHandler value2 =
                        (RemovedItemEventHandler) Delegate.Combine(removedItemEventHandler2, value);
                    removedItemEventHandler =
                        Interlocked.CompareExchange(ref RemovedItemEvent, value2, removedItemEventHandler2);
                } while ((object) removedItemEventHandler != removedItemEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                RemovedItemEventHandler removedItemEventHandler = RemovedItemEvent;
                RemovedItemEventHandler removedItemEventHandler2;
                do
                {
                    removedItemEventHandler2 = removedItemEventHandler;
                    RemovedItemEventHandler value2 =
                        (RemovedItemEventHandler) Delegate.Remove(removedItemEventHandler2, value);
                    removedItemEventHandler =
                        Interlocked.CompareExchange(ref RemovedItemEvent, value2, removedItemEventHandler2);
                } while ((object) removedItemEventHandler != removedItemEventHandler2);
            }
        }

        public event RemovedLocationEventHandler RemovedLocation
        {
            [CompilerGenerated]
            add
            {
                RemovedLocationEventHandler removedLocationEventHandler = RemovedLocationEvent;
                RemovedLocationEventHandler removedLocationEventHandler2;
                do
                {
                    removedLocationEventHandler2 = removedLocationEventHandler;
                    RemovedLocationEventHandler value2 =
                        (RemovedLocationEventHandler) Delegate.Combine(removedLocationEventHandler2, value);
                    removedLocationEventHandler = Interlocked.CompareExchange(ref RemovedLocationEvent, value2,
                        removedLocationEventHandler2);
                } while ((object) removedLocationEventHandler != removedLocationEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                RemovedLocationEventHandler removedLocationEventHandler = RemovedLocationEvent;
                RemovedLocationEventHandler removedLocationEventHandler2;
                do
                {
                    removedLocationEventHandler2 = removedLocationEventHandler;
                    RemovedLocationEventHandler value2 =
                        (RemovedLocationEventHandler) Delegate.Remove(removedLocationEventHandler2, value);
                    removedLocationEventHandler = Interlocked.CompareExchange(ref RemovedLocationEvent, value2,
                        removedLocationEventHandler2);
                } while ((object) removedLocationEventHandler != removedLocationEventHandler2);
            }
        }

        public event HideSplashEventHandler HideSplash
        {
            [CompilerGenerated]
            add
            {
                HideSplashEventHandler hideSplashEventHandler = HideSplashEvent;
                HideSplashEventHandler hideSplashEventHandler2;
                do
                {
                    hideSplashEventHandler2 = hideSplashEventHandler;
                    HideSplashEventHandler value2 =
                        (HideSplashEventHandler) Delegate.Combine(hideSplashEventHandler2, value);
                    hideSplashEventHandler =
                        Interlocked.CompareExchange(ref HideSplashEvent, value2, hideSplashEventHandler2);
                } while ((object) hideSplashEventHandler != hideSplashEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                HideSplashEventHandler hideSplashEventHandler = HideSplashEvent;
                HideSplashEventHandler hideSplashEventHandler2;
                do
                {
                    hideSplashEventHandler2 = hideSplashEventHandler;
                    HideSplashEventHandler value2 =
                        (HideSplashEventHandler) Delegate.Remove(hideSplashEventHandler2, value);
                    hideSplashEventHandler =
                        Interlocked.CompareExchange(ref HideSplashEvent, value2, hideSplashEventHandler2);
                } while ((object) hideSplashEventHandler != hideSplashEventHandler2);
            }
        }

        public event SavedNoteEventHandler SavedNote
        {
            [CompilerGenerated]
            add
            {
                SavedNoteEventHandler savedNoteEventHandler = SavedNoteEvent;
                SavedNoteEventHandler savedNoteEventHandler2;
                do
                {
                    savedNoteEventHandler2 = savedNoteEventHandler;
                    SavedNoteEventHandler value2 =
                        (SavedNoteEventHandler) Delegate.Combine(savedNoteEventHandler2, value);
                    savedNoteEventHandler =
                        Interlocked.CompareExchange(ref SavedNoteEvent, value2, savedNoteEventHandler2);
                } while ((object) savedNoteEventHandler != savedNoteEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                SavedNoteEventHandler savedNoteEventHandler = SavedNoteEvent;
                SavedNoteEventHandler savedNoteEventHandler2;
                do
                {
                    savedNoteEventHandler2 = savedNoteEventHandler;
                    SavedNoteEventHandler value2 =
                        (SavedNoteEventHandler) Delegate.Remove(savedNoteEventHandler2, value);
                    savedNoteEventHandler =
                        Interlocked.CompareExchange(ref SavedNoteEvent, value2, savedNoteEventHandler2);
                } while ((object) savedNoteEventHandler != savedNoteEventHandler2);
            }
        }

        public event SavedSceneEventHandler SavedScene
        {
            [CompilerGenerated]
            add
            {
                SavedSceneEventHandler savedSceneEventHandler = SavedSceneEvent;
                SavedSceneEventHandler savedSceneEventHandler2;
                do
                {
                    savedSceneEventHandler2 = savedSceneEventHandler;
                    SavedSceneEventHandler value2 =
                        (SavedSceneEventHandler) Delegate.Combine(savedSceneEventHandler2, value);
                    savedSceneEventHandler =
                        Interlocked.CompareExchange(ref SavedSceneEvent, value2, savedSceneEventHandler2);
                } while ((object) savedSceneEventHandler != savedSceneEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                SavedSceneEventHandler savedSceneEventHandler = SavedSceneEvent;
                SavedSceneEventHandler savedSceneEventHandler2;
                do
                {
                    savedSceneEventHandler2 = savedSceneEventHandler;
                    SavedSceneEventHandler value2 =
                        (SavedSceneEventHandler) Delegate.Remove(savedSceneEventHandler2, value);
                    savedSceneEventHandler =
                        Interlocked.CompareExchange(ref SavedSceneEvent, value2, savedSceneEventHandler2);
                } while ((object) savedSceneEventHandler != savedSceneEventHandler2);
            }
        }

        public event SavedCharEventHandler SavedChar
        {
            [CompilerGenerated]
            add
            {
                SavedCharEventHandler savedCharEventHandler = SavedCharEvent;
                SavedCharEventHandler savedCharEventHandler2;
                do
                {
                    savedCharEventHandler2 = savedCharEventHandler;
                    SavedCharEventHandler value2 =
                        (SavedCharEventHandler) Delegate.Combine(savedCharEventHandler2, value);
                    savedCharEventHandler =
                        Interlocked.CompareExchange(ref SavedCharEvent, value2, savedCharEventHandler2);
                } while ((object) savedCharEventHandler != savedCharEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                SavedCharEventHandler savedCharEventHandler = SavedCharEvent;
                SavedCharEventHandler savedCharEventHandler2;
                do
                {
                    savedCharEventHandler2 = savedCharEventHandler;
                    SavedCharEventHandler value2 =
                        (SavedCharEventHandler) Delegate.Remove(savedCharEventHandler2, value);
                    savedCharEventHandler =
                        Interlocked.CompareExchange(ref SavedCharEvent, value2, savedCharEventHandler2);
                } while ((object) savedCharEventHandler != savedCharEventHandler2);
            }
        }

        public event SavedChapterEventHandler SavedChapter
        {
            [CompilerGenerated]
            add
            {
                SavedChapterEventHandler savedChapterEventHandler = SavedChapterEvent;
                SavedChapterEventHandler savedChapterEventHandler2;
                do
                {
                    savedChapterEventHandler2 = savedChapterEventHandler;
                    SavedChapterEventHandler value2 =
                        (SavedChapterEventHandler) Delegate.Combine(savedChapterEventHandler2, value);
                    savedChapterEventHandler =
                        Interlocked.CompareExchange(ref SavedChapterEvent, value2, savedChapterEventHandler2);
                } while ((object) savedChapterEventHandler != savedChapterEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                SavedChapterEventHandler savedChapterEventHandler = SavedChapterEvent;
                SavedChapterEventHandler savedChapterEventHandler2;
                do
                {
                    savedChapterEventHandler2 = savedChapterEventHandler;
                    SavedChapterEventHandler value2 =
                        (SavedChapterEventHandler) Delegate.Remove(savedChapterEventHandler2, value);
                    savedChapterEventHandler =
                        Interlocked.CompareExchange(ref SavedChapterEvent, value2, savedChapterEventHandler2);
                } while ((object) savedChapterEventHandler != savedChapterEventHandler2);
            }
        }

        public event SavedItemEventHandler SavedItem
        {
            [CompilerGenerated]
            add
            {
                SavedItemEventHandler savedItemEventHandler = SavedItemEvent;
                SavedItemEventHandler savedItemEventHandler2;
                do
                {
                    savedItemEventHandler2 = savedItemEventHandler;
                    SavedItemEventHandler value2 =
                        (SavedItemEventHandler) Delegate.Combine(savedItemEventHandler2, value);
                    savedItemEventHandler =
                        Interlocked.CompareExchange(ref SavedItemEvent, value2, savedItemEventHandler2);
                } while ((object) savedItemEventHandler != savedItemEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                SavedItemEventHandler savedItemEventHandler = SavedItemEvent;
                SavedItemEventHandler savedItemEventHandler2;
                do
                {
                    savedItemEventHandler2 = savedItemEventHandler;
                    SavedItemEventHandler value2 =
                        (SavedItemEventHandler) Delegate.Remove(savedItemEventHandler2, value);
                    savedItemEventHandler =
                        Interlocked.CompareExchange(ref SavedItemEvent, value2, savedItemEventHandler2);
                } while ((object) savedItemEventHandler != savedItemEventHandler2);
            }
        }

        public event SavedLocationEventHandler SavedLocation
        {
            [CompilerGenerated]
            add
            {
                SavedLocationEventHandler savedLocationEventHandler = SavedLocationEvent;
                SavedLocationEventHandler savedLocationEventHandler2;
                do
                {
                    savedLocationEventHandler2 = savedLocationEventHandler;
                    SavedLocationEventHandler value2 =
                        (SavedLocationEventHandler) Delegate.Combine(savedLocationEventHandler2, value);
                    savedLocationEventHandler =
                        Interlocked.CompareExchange(ref SavedLocationEvent, value2, savedLocationEventHandler2);
                } while ((object) savedLocationEventHandler != savedLocationEventHandler2);
            }
            [CompilerGenerated]
            remove
            {
                SavedLocationEventHandler savedLocationEventHandler = SavedLocationEvent;
                SavedLocationEventHandler savedLocationEventHandler2;
                do
                {
                    savedLocationEventHandler2 = savedLocationEventHandler;
                    SavedLocationEventHandler value2 =
                        (SavedLocationEventHandler) Delegate.Remove(savedLocationEventHandler2, value);
                    savedLocationEventHandler =
                        Interlocked.CompareExchange(ref SavedLocationEvent, value2, savedLocationEventHandler2);
                } while ((object) savedLocationEventHandler != savedLocationEventHandler2);
            }
        }

        public clsProject()
        {
            cXml = new clsXml();
            cProjectIO = new clsProjectIO();
            colSaveTokens = new Collection();
            strSaveToken = "Token";
            cVars = new clsVars();
            cExports = new clsExports();
            cChapters = new clsChapters();
            cLocations = new clsLocations();
            cItems = new clsItems();
            cChars = new clsCharacters();
            cNotes = new clsNotes();
            cScenes = new clsScenes();
            cBooks = new clsBooks();
            cParts = new clsParts();
            mvarXMLFilename = "";
            mvarTITLE = "";
            mvarBio = "";
            mvarAuthorname = "";
            mvarDesc = "";
            mvarLatexHeaderFile = "";
            mvarDeadlineDate = new DateTime[7];
            mvarDeadlineStartDate = new DateTime[7];
            mvarFieldTitle = new string[6];
            mvarPassword = "";
            mvarCollabFolder = "";
            mvarLastFullBackupFilename = "";
            mvarWordLogs = new clsWordLogs();
            clsShared.cLog.AddAction("YW5Lib.dll: " + Assembly.GetExecutingAssembly().GetName().Version.ToString());
            SaveTimer = new System.Windows.Forms.Timer();
            SaveTimer.Interval = 300000;
            SaveTimer.Start();
        }

        public clsCollection<int> AllChapterIDs()
        {
            clsCollection<int> clsCollection = new clsCollection<int>();
            int chapterCount = ChapterCount;
            for (int i = 1; i <= chapterCount; i++)
            {
                int iD = ((clsGenericBase<clsChapter>) Chapters).get_ByIndex(i).ID;
                clsCollection.Add(iD, Conversions.ToString(iD));
            }

            return clsCollection;
        }

        public bool NotBlank()
        {
            if (Operators.CompareString(mvarXMLFilename, "", TextCompare: false) != 0)
            {
                return true;
            }

            return false;
        }

        public int Wordcount()
        {
            int num = 0;
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = cChapters.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    clsChapter clsChapter = (clsChapter) enumerator.Current;
                    num += this.get_ChapterWordcount(clsChapter.ID, WithUnused: false);
                }

                return num;
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public int WordcountWithUnused()
        {
            int num = 0;
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = cChapters.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    clsChapter clsChapter = (clsChapter) enumerator.Current;
                    num += this.get_ChapterWordcount(clsChapter.ID, WithUnused: true);
                }

                return num;
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void MoveSceneUp(int ScID)
        {
            bool flag = false;
            clsScene clsScene = this.get_SceneByID(ScID);
            int belongsToChID = this.get_SceneByID(ScID).BelongsToChID;
            clsChapter clsChapter = this.get_ChapterByID(belongsToChID);
            int num = clsChapter.SceneIndex(ScID);
            int num2 = ChapterIndex(belongsToChID);
            if (num2 == 1)
            {
                flag = true;
            }

            if (num == 1)
            {
                if (ChapterCount > 1)
                {
                    if (flag)
                    {
                        int num3 = clsScene.BelongsToChID = this.get_ChapterByIndex(ChapterCount).ID;
                        clsChapter.RemoveSceneByID(ScID);
                        clsScene.BelongsToChID = num3;
                        this.get_ChapterByID(num3).AddSceneID(ScID);
                    }
                    else
                    {
                        clsChapter.RemoveSceneByID(ScID);
                        this.get_ChapterByIndex(num2 - 1).AddSceneID(ScID);
                        clsScene.BelongsToChID = this.get_ChapterByIndex(num2 - 1).ID;
                    }
                }
            }
            else
            {
                int beforeScID = clsChapter.get_SceneIDByIndex(num - 1);
                clsChapter.RemoveSceneByID(ScID);
                clsChapter.InsertSceneBeforeID(ScID, beforeScID);
            }
        }

        public void MoveSceneDown(int ScID)
        {
            bool flag = false;
            clsScene clsScene = this.get_SceneByID(ScID);
            int belongsToChID = this.get_SceneByID(ScID).BelongsToChID;
            clsChapter clsChapter = this.get_ChapterByID(belongsToChID);
            int num = clsChapter.SceneIndex(ScID);
            int num2 = ChapterIndex(belongsToChID);
            if (num2 == ChapterCount)
            {
                flag = true;
            }

            if (num == clsChapter.SceneCount)
            {
                if (ChapterCount > 1)
                {
                    if (flag)
                    {
                        int num3 = clsScene.BelongsToChID = this.get_ChapterByIndex(1).ID;
                        clsChapter.RemoveSceneByID(ScID);
                        clsScene.BelongsToChID = num3;
                        this.get_ChapterByID(num3).InsertSceneIDBeforeIndex(ScID, 1);
                    }
                    else
                    {
                        clsChapter.RemoveSceneByID(ScID);
                        this.get_ChapterByIndex(num2 + 1).InsertSceneIDBeforeIndex(ScID, 1);
                        clsScene.BelongsToChID = this.get_ChapterByIndex(num2 + 1).ID;
                    }
                }
            }
            else
            {
                int afterID = clsChapter.get_SceneIDByIndex(num + 1);
                clsChapter.RemoveSceneByID(ScID);
                clsChapter.AppendSceneAfterID(ref ScID, afterID);
            }
        }

        public int ChapterIndex(int ChID)
        {
            int result = -1;
            int chapterCount = ChapterCount;
            for (int i = 1; i <= chapterCount; i++)
            {
                if (Chapters.ChapterByIndex(i).ID == ChID)
                {
                    result = i;
                    break;
                }
            }

            return result;
        }

        public clsCollection<int> SelectedChapterIDs(string ChIDsSemiColonSeparated, bool WithUnused)
        {
            clsCollection<int> clsCollection = new clsCollection<int>();
            clsCollection<int> clsCollection2 = new clsCollection<int>();
            string[] array = ChIDsSemiColonSeparated.Split(new string[1]
            {
                ";"
            }, StringSplitOptions.RemoveEmptyEntries);
            int num = array.Length - 1;
            for (int i = 0; i <= num; i++)
            {
                int num2 = Conversions.ToInteger(array[i]);
                if (num2 > 0 && (WithUnused | !this.get_ChapterByID(num2).Unused))
                {
                    clsCollection.Add(num2, Conversions.ToString(num2));
                }
            }

            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = Chapters.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    clsChapter clsChapter = (clsChapter) enumerator.Current;
                    if (clsCollection.Contains(clsChapter.ID))
                    {
                        clsCollection2.Add(clsChapter.ID, Conversions.ToString(clsChapter.ID));
                    }
                }

                return clsCollection2;
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void RemoveChapterOnlyByID(int ID)
        {
            cChapters.RemoveChapterByID(ID);
            RemovedChapterEvent?.Invoke(ID);
        }

        public void RemoveChapterANDScenesByID(int ChapterID)
        {
            int sceneCount = cChapters.ChapterByID(ChapterID).SceneCount;
            for (int i = 1; i <= sceneCount; i++)
            {
                RemoveSceneByID(cChapters.ChapterByID(ChapterID).get_SceneIDByIndex(i));
            }

            cChapters.RemoveChapterByID(ChapterID);
            RemovedChapterEvent?.Invoke(ChapterID);
        }

        public void RemoveCharByID(int ID)
        {
            try
            {
                int sceneCount = SceneCount;
                for (int i = 1; i <= sceneCount; i++)
                {
                    this.get_SceneByIndex(i).RemoveCharByID(ID);
                }

                cChars.RemoveCharByID(ID);
                RemovedCharEvent?.Invoke(ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void RemoveSceneByID(int ID)
        {
            try
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = Chapters.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        ((clsChapter) enumerator.Current).RemoveSceneByID(ID);
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }

                cScenes.RemoveSceneByID(ID);
                RemovedSceneEvent?.Invoke(ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void RemoveLocationByID(int ID)
        {
            try
            {
                int sceneCount = SceneCount;
                for (int i = 1; i <= sceneCount; i++)
                {
                    this.get_SceneByIndex(i).RemoveLocationByID(ID);
                }

                cLocations.RemoveLocationByID(ID);
                RemovedLocationEvent?.Invoke(ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void RemoveItemByID(int ID)
        {
            try
            {
                int sceneCount = SceneCount;
                for (int i = 1; i <= sceneCount; i++)
                {
                    this.get_SceneByIndex(i).RemoveItemByID(ID);
                }

                cItems.RemoveByID(ID);
                RemovedItemEvent?.Invoke(ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void RemoveNoteByID(int ID)
        {
            cNotes.RemoveNoteByID(ID);
        }

        public string ProjectPath()
        {
            return clsShared.cFile.PathOnly(mvarXMLFilename);
        }

        public string ExportFolder()
        {
            return Path.Combine(clsShared.cFile.PathOnly(mvarXMLFilename), "Export");
        }

        public bool Load(string ProjectFilename, bool IgnoreLock)
        {
            bool flag = default(bool);
            try
            {
                string left = clsShared.cFile.ExtOnly(ProjectFilename).ToLower();
                if (Operators.CompareString(left, "yw3", TextCompare: false) != 0)
                {
                    if (!((Operators.CompareString(left, "yw4", TextCompare: false) == 0) |
                          (Operators.CompareString(left, "ywc4", TextCompare: false) == 0)))
                    {
                        if (Strings.InStr(ProjectFilename, ".zip\\", CompareMethod.Text) <= 0)
                        {
                            if (!((Operators.CompareString(left, "yw5", TextCompare: false) == 0) &
                                  !cProjectIO.FolderExists(modSubs.RTFPathV5(clsShared.cFile.PathOnly(ProjectFilename)))
                                ))
                            {
                                if (Operators.CompareString(left, "yw6", TextCompare: false) != 0)
                                {
                                    if (!((Operators.CompareString(left, "yw5", TextCompare: false) != 0) &
                                          (Operators.CompareString(left, "ywc5", TextCompare: false) != 0)))
                                    {
                                        flag = LoadYW5(ProjectFilename, IgnoreLock);
                                        if (!flag)
                                        {
                                            return flag;
                                        }

                                        if (Operators.CompareString(left, "ywc5", TextCompare: false) != 0 ||
                                            IgnoreLock)
                                        {
                                            return flag;
                                        }

                                        clsShared.cLog.AddAction("Loaded a yWC5 file");
                                        Interaction.MsgBox(
                                            "IMPORTANT!\r\nYou've loaded an all-in-one 'ywc' project file. When it saves, you'll need to specify a new project folder to save it in. It'll be saved as a regular 'yw5' file with separate scene RTF files. If you want to update the ywc5 file instead, you need to click 'Tools' then 'Backups' then 'Save project content to single file'");
                                        return flag;
                                    }

                                    if (Operators.CompareString(clsShared.cFile.ExtOnly(ProjectFilename).ToLower(),
                                            "bak", TextCompare: false) != 0)
                                    {
                                        clsShared.cLog.ShowMinorError(
                                            clsShared.cFile.FileOnly(ProjectFilename) +
                                            " is not a valid project file. Load cancelled.");
                                        return flag;
                                    }

                                    string text = Strings.Replace(clsShared.cFile.FileOnly(ProjectFilename), ".bak", "",
                                        1, -1, CompareMethod.Text);
                                    Interaction.MsgBox(
                                        "The file you're trying to load is a backup copy (.yw5.bak) of an original project (.yw5)\r\nI strongly recommend you open the project folder and compare the file dates of " +
                                        text + " and " + clsShared.cFile.FileOnly(ProjectFilename) +
                                        "\r\nIf the file '" + clsShared.cFile.FileOnly(ProjectFilename) +
                                        "' is newer, you need to rename it and remove the .bak file extension before loading it again.");
                                    return flag;
                                }

                                flag = LoadYW6(ProjectFilename, IgnoreLock);
                                return flag;
                            }

                            clsShared.cLog.AddAction("Cannot load project " + ProjectFilename +
                                                     ": There is no RTF5 folder in the project path.");
                            Interaction.MsgBox(
                                "Cannot load your project: There is no RTF5 folder in the project path.\r\n(This vital folder holds all the files with your scene content, and must be in the same location as your YW5 project file.)\r\nIf you're trying to load a project from a Zip archive, you must extract the entire archive first.",
                                MsgBoxStyle.Exclamation);
                            return flag;
                        }

                        Interaction.MsgBox(
                            "You can't load a yWriter project from a zip archive. Please extract the zip contents first.");
                        return flag;
                    }

                    Interaction.MsgBox("Can't load YW4 files directly - please use the Import YW4 routine instead.");
                    return flag;
                }

                Interaction.MsgBox("Can't load YW3 files directly - please use the Import YW3 routine instead.");
                return flag;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return flag;
            }
        }

        private bool LoadYW5(string ProjectFilename, bool IgnoreLock)
        {
            //Discarded unreachable code: IL_0c20, IL_0c3a, IL_0c48, IL_0c53, IL_0c60, IL_0ee6, IL_0ee8, IL_0efc, IL_0f1e
            int num = default(int);
            bool flag = false;
            int num3 = default(int);
            try
            {
                ProjectData.ClearProjectError();
                num = 2;
                int num2 = 2;
                Clear(FilenameToo: true);
                num2 = 3;
                flag = true;
                num2 = 4;
                mvarV6 = false;
                num2 = 5;
                bool flag2 = false;
                num2 = 6;
                clsShared.cSubs.BusyMouse();
                num2 = 7;
                bool flag3 = default(bool);
                if (!IgnoreLock)
                {
                    num2 = 8;
                    if (cProjectIO.ProjectIsLocked(ProjectFilename))
                    {
                        num2 = 9;
                        flag3 = true;
                        num2 = 10;
                        clsShared.cLog.AddAction("Project " + ProjectFilename + " is already locked. Cannot load it.");
                        num2 = 11;
                        flag = false;
                    }
                }

                num2 = 12;
                string TextString = "";
                num2 = 13;
                mvarLoading = true;
                num2 = 14;
                if (flag)
                {
                    num2 = 15;
                    ProgressEvent?.Invoke("Loading Project");
                }

                num2 = 16;
                if (flag)
                {
                    num2 = 17;
                    mvarXMLFilename = ProjectFilename;
                }

                num2 = 18;
                if (!IgnoreLock)
                {
                    num2 = 19;
                    if (flag)
                    {
                        num2 = 20;
                        if (!cProjectIO.LockProject(ProjectFilename))
                        {
                            num2 = 21;
                            clsShared.cLog.AddAction("Unable to lock project " + ProjectFilename +
                                                     " - already open elsewhere?");
                        }
                    }
                }

                num2 = 22;
                string text = clsShared.cFile.PathOnly(ProjectFilename);
                num2 = 23;
                string text2 = modSubs.RTFPathV5(text);
                num2 = 24;
                bool rTFIncluded = default(bool);
                if (flag)
                {
                    num2 = 25;
                    clsShared.cLog.AddAction("Loading from " + text);
                    num2 = 26;
                    if (!clsShared.cFile.ReadTextAuto(FileWithPath, ref TextString, UseSysEncoding: true))
                    {
                        num2 = 27;
                        clsShared.cLog.AddAction("Could not read from " + FileWithPath);
                        num2 = 28;
                        flag = false;
                    }
                    else
                    {
                        num2 = 30;
                        if (mvarXMLFilename.ToLower().EndsWith("ywc5"))
                        {
                            num2 = 31;
                            rTFIncluded = true;
                        }
                    }
                }

                num2 = 32;
                if (flag)
                {
                    num2 = 33;
                    if (!clsShared.cFile.FolderExists(text2) & clsShared.cFile.FolderExists(Path.Combine(text, "RTF")))
                    {
                        num2 = 34;
                        clsShared.cLog.AddAction("Renaming RTF folder to RTF5");
                        num2 = 35;
                        clsShared.cFile.MoveFolder(Path.Combine(text, "RTF"), text2);
                    }
                }

                num2 = 36;
                if (flag)
                {
                    num2 = 37;
                    string text3 = cXml.Extract(TextString, "<?xml", "?>");
                    num2 = 38;
                    text3 = Strings.Mid(text3, Strings.InStrRev(text3, "=") + 1);
                    num2 = 39;
                    text3 = Strings.Replace(text3, "\"", "");
                    num2 = 40;
                    if (Operators.CompareString(text3.ToLower(), Encoding.Default.BodyName, TextCompare: false) != 0)
                    {
                        num2 = 41;
                        if (Interaction.MsgBox(
                                "WARNING! File was saved with encoding '" + text3.ToLower() +
                                "' but your version of windows is using encoding '" + Encoding.Default.BodyName +
                                "'\r\nIf you continue, your data might get corrupted. Are you sure you want to open this file?",
                                MsgBoxStyle.YesNo | MsgBoxStyle.Question) == MsgBoxResult.No)
                        {
                            num2 = 42;
                            clsShared.cLog.AddError(
                                "User cancelled loading - File was saved with encoding '" + text3.ToLower() +
                                "' but this version of windows is using encoding '" + Encoding.Default.BodyName + "'",
                                MethodBase.GetCurrentMethod());
                            num2 = 43;
                            flag = false;
                        }
                    }

                    num2 = 44;
                    if (flag)
                    {
                        num2 = 45;
                        string xMLData = cXml.ExtractWithTags(TextString, "<PROJECT>", "</PROJECT>");
                        num2 = 46;
                        if (!CreateFromXML(xMLData))
                        {
                            num2 = 47;
                            clsShared.cLog.AddError(
                                "Load failed ... unable to extract project data from " + FileWithPath,
                                MethodBase.GetCurrentMethod());
                            num2 = 48;
                            flag = false;
                        }
                        else
                        {
                            num2 = 50;
                            if (mvarXMLFileVersion < 3)
                            {
                                num2 = 51;
                                clsShared.cFile.ReadTextAuto(FileWithPath, ref TextString, UseSysEncoding: true);
                                num2 = 52;
                                xMLData = cXml.ExtractWithTags(TextString, "<PROJECT>", "</PROJECT>");
                                num2 = 53;
                                if (!CreateFromXML(xMLData))
                                {
                                    num2 = 54;
                                    clsShared.cLog.AddError(
                                        "Load failed ... unable to extract project data from unicode file " +
                                        FileWithPath, MethodBase.GetCurrentMethod());
                                    num2 = 55;
                                    flag = false;
                                }
                            }
                        }
                    }
                }

                num2 = 56;
                if (flag)
                {
                    num2 = 57;
                    if (UsePassword)
                    {
                        num2 = 58;
                        frmPasswordEntry frmPasswordEntry = new frmPasswordEntry(Password);
                        num2 = 59;
                        frmPasswordEntry.ShowDialog();
                        num2 = 60;
                        if (!frmPasswordEntry.PasswordOk)
                        {
                            num2 = 61;
                            flag = false;
                            num2 = 62;
                            flag2 = true;
                            num2 = 63;
                            clsShared.cLog.AddAction(
                                "Project load cancelled - password protected, user did not enter the password.");
                            num2 = 64;
                            Interaction.MsgBox("Project load cancelled.");
                        }
                    }
                }

                num2 = 65;
                if (flag)
                {
                    num2 = 66;
                    ProgressEvent?.Invoke("Loading Books");
                    num2 = 67;
                    string xMLData = cXml.ExtractWithTags(TextString, "<PROJECTBOOKS>", "</PROJECTBOOKS>");
                    num2 = 68;
                    if (!cBooks.CreateFromXML(xMLData))
                    {
                        num2 = 69;
                        clsShared.cLog.AddAction("Unable to extract book data from " + FileWithPath);
                    }
                }

                num2 = 70;
                if (flag)
                {
                    num2 = 71;
                    ProgressEvent?.Invoke("Loading Parts");
                    num2 = 72;
                    string xMLData = cXml.ExtractWithTags(TextString, "<PROJECTPARTS>", "</PROJECTPARTS>");
                    num2 = 73;
                    if (!cParts.CreateFromXML(xMLData))
                    {
                        num2 = 74;
                        clsShared.cLog.AddAction("Unable to extract book data from " + FileWithPath);
                    }
                }

                num2 = 75;
                if (flag)
                {
                    num2 = 76;
                    ProgressEvent?.Invoke("Loading Chapters");
                    num2 = 77;
                    string xMLData = cXml.ExtractWithTags(TextString, "<CHAPTERS>", "</CHAPTERS>");
                    num2 = 78;
                    if (!cChapters.CreateFromXML(xMLData))
                    {
                        num2 = 79;
                        clsShared.cLog.AddAction("Load failed ... unable to extract chapter data from " + FileWithPath);
                        num2 = 80;
                        flag = false;
                    }
                }

                num2 = 81;
                if (flag)
                {
                    num2 = 82;
                    ProgressEvent?.Invoke("Loading Scenes");
                    num2 = 83;
                    string xMLData = cXml.ExtractWithTags(TextString, "<SCENES>", "</SCENES>");
                    num2 = 84;
                    if (!cScenes.CreateFromXML(text, xMLData, rTFIncluded, TriggerSaveEvent: false, V6File))
                    {
                        num2 = 85;
                        clsShared.cLog.AddAction("Load failed ... unable to extract scene data / load scenes from " +
                                                 FileWithPath);
                        num2 = 86;
                        flag = false;
                    }
                }

                num2 = 87;
                if (flag)
                {
                    num2 = 88;
                    ProgressEvent?.Invoke("Loading Locations");
                    num2 = 89;
                    string xMLData = cXml.ExtractWithTags(TextString, "<LOCATIONS>", "</LOCATIONS>");
                    num2 = 90;
                    if (!cLocations.CreateFromXML(xMLData))
                    {
                        num2 = 91;
                        clsShared.cLog.AddAction("Load failed ... unable to extract location data from " +
                                                 FileWithPath);
                        num2 = 92;
                        flag = false;
                    }
                }

                num2 = 93;
                if (flag)
                {
                    num2 = 94;
                    ProgressEvent?.Invoke("Loading Characters");
                    num2 = 95;
                    string xMLData = cXml.ExtractWithTags(TextString, "<CHARACTERS>", "</CHARACTERS>");
                    num2 = 96;
                    if (!cChars.CreateFromXML(xMLData))
                    {
                        num2 = 97;
                        clsShared.cLog.AddAction(
                            "Load failed ... unable to extract character data from " + FileWithPath);
                        num2 = 98;
                        flag = false;
                    }
                }

                num2 = 99;
                if (flag)
                {
                    num2 = 100;
                    ProgressEvent?.Invoke("Loading Items");
                    num2 = 101;
                    string xMLData = cXml.ExtractWithTags(TextString, "<ITEMS>", "</ITEMS>");
                    num2 = 102;
                    if (!cItems.CreateFromXML(xMLData))
                    {
                        num2 = 103;
                        clsShared.cLog.AddAction("Load failed ... unable to extract items data from " + FileWithPath);
                        num2 = 104;
                        flag = false;
                    }
                }

                num2 = 105;
                if (flag)
                {
                    num2 = 106;
                    ProgressEvent?.Invoke("Loading Notes");
                    num2 = 107;
                    string xMLData = cXml.ExtractWithTags(TextString, "<PROJECTNOTES>", "</PROJECTNOTES>");
                    num2 = 108;
                    if (!cNotes.CreateFromXML(xMLData))
                    {
                        num2 = 109;
                        clsShared.cLog.AddAction("Unable to extract notes data from " + FileWithPath);
                    }
                }

                num2 = 110;
                if (flag)
                {
                    num2 = 111;
                    ProgressEvent?.Invoke("Loading Variables");
                    num2 = 112;
                    string xMLData = cXml.ExtractWithTags(TextString, "<PROJECTVARS>", "</PROJECTVARS>");
                    num2 = 113;
                    if (!cVars.CreateFromXML(xMLData))
                    {
                        num2 = 114;
                        clsShared.cLog.AddAction("Unable to extract variables from " + FileWithPath);
                    }
                }

                num2 = 115;
                if (flag)
                {
                    num2 = 116;
                    ProgressEvent?.Invoke("Loading Custom Exports");
                    num2 = 117;
                    string xMLData = cXml.ExtractWithTags(TextString, "<PROJECTEXPS>", "</PROJECTEXPS>");
                    num2 = 118;
                    if (!cExports.CreateFromXML(xMLData))
                    {
                        num2 = 119;
                        clsShared.cLog.AddAction("Unable to extract Custom Exports from " + FileWithPath);
                    }
                }

                num2 = 120;
                if (flag)
                {
                    num2 = 121;
                    ProgressEvent?.Invoke("Loading Wordcount Logs");
                    num2 = 122;
                    string xMLData = cXml.ExtractWithTags(TextString, "<WCLog>", "</WCLog>");
                    num2 = 123;
                    if (!mvarWordLogs.CreateFromXML(xMLData))
                    {
                        num2 = 124;
                        clsShared.cLog.AddAction("Unable to extract wordcount log data from " + FileWithPath);
                    }
                }

                num2 = 125;
                if (!flag)
                {
                    num2 = 126;
                    cProjectIO.UnlockProject(ProjectFilename);
                    num2 = 127;
                    Clear(FilenameToo: true);
                    num2 = 128;
                    clsShared.cSubs.NormalMouse();
                    num2 = 129;
                    if (flag3)
                    {
                        num2 = 130;
                        Interaction.MsgBox(
                            "This project is already open in another copy of yWriter.\r\nIf you don't think this is the case, please close ALL instances of yWriter and try again. (If you are certain, manually delete the LOCK file in the project folder before reloading your project.)",
                            MsgBoxStyle.Exclamation, "Unable to load " + ProjectFilename);
                    }
                    else
                    {
                        num2 = 132;
                        HideSplashEvent?.Invoke();
                        num2 = 133;
                        if (!flag2)
                        {
                            num2 = 134;
                            clsShared.cShowError.ShowErr(
                                "There was an error loading this project. Please submit an error report - and if possible, email your YW5 project file to spacejock @ gmail.com",
                                MethodBase.GetCurrentMethod(), "");
                        }
                    }
                }
                else
                {
                    num2 = 136;
                    IEnumerator enumerator = Chapters.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsChapter clsChapter = (clsChapter) enumerator.Current;
                        num2 = 137;
                        int sceneCount = clsChapter.SceneCount;
                        for (int i = 1; i <= sceneCount; i++)
                        {
                            num2 = 138;
                            clsScene clsScene = this.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
                            num2 = 139;
                            if (clsScene.BelongsToChID != clsChapter.ID)
                            {
                                num2 = 140;
                                clsShared.cLog.AddAction("Scene ID " + Conversions.ToString(clsScene.ID) +
                                                         " says it belongs to chapter ID " +
                                                         Conversions.ToString(clsScene.BelongsToChID) +
                                                         " but should be " + Conversions.ToString(clsChapter.ID) +
                                                         ". Fixed");
                                num2 = 141;
                                clsScene.BelongsToChID = clsChapter.ID;
                            }

                            num2 = 142;
                        }

                        num2 = 143;
                    }

                    num2 = 144;
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }

                    num2 = 145;
                    ProgressEvent?.Invoke("Loading Complete");
                    num2 = 146;
                    if (!ProjectIsSane())
                    {
                        num2 = 147;
                        clsShared.cShowError.ShowErr("Indexing error in project file", MethodBase.GetCurrentMethod(),
                            "");
                    }
                    else
                    {
                        num2 = 149;
                        MakeSubFolders();
                        num2 = 150;
                        ProgressEvent?.Invoke("Creating autobackups");
                        num2 = 151;
                        BackupProject(WithSceneContent: false);
                    }
                }

                num2 = 152;
                mvarLoading = false;
                num2 = 153;
                clsShared.cSubs.NormalMouse();
            }
            catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
            {
                ProjectData.SetProjectError((Exception) obj);
                /*Error near IL_0f1c: Could not find block for branch target IL_0ee8*/
                ;
            }

            if (num3 != 0)
            {
                ProjectData.ClearProjectError();
            }

            return flag;
        }

        private bool LoadYW6(string ProjectFilename, bool IgnoreLock)
        {
            bool flag = true;
            mvarV6 = true;
            Clear(FilenameToo: true);
            try
            {
                bool flag2 = false;
                clsShared.cSubs.BusyMouse();
                bool flag3 = default(bool);
                if (!IgnoreLock && cProjectIO.ProjectIsLocked(ProjectFilename))
                {
                    flag3 = true;
                    clsShared.cLog.AddAction("Project " + ProjectFilename + " is already locked. Cannot load it.");
                    flag = false;
                }

                string text = "";
                mvarLoading = true;
                if (flag)
                {
                    ProgressEvent?.Invoke("Loading YW6 Project");
                }

                if (flag)
                {
                    mvarXMLFilename = ProjectFilename;
                }

                string str = clsShared.cFile.PathOnly(ProjectFilename);
                if (flag)
                {
                    clsShared.cLog.AddAction("Loading from " + str);
                    text = cProjectIO.LoadProjV6(FileWithPath);
                    mvarV6 = true;
                    flag = LoadProjectFromXML(text);
                }

                if (!IgnoreLock && flag && !cProjectIO.LockProject(ProjectFilename))
                {
                    clsShared.cLog.AddAction("Unable to lock project " + ProjectFilename +
                                             " - already open elsewhere?");
                }

                if (!flag)
                {
                    UnlockProject(ProjectFilename);
                    Clear(FilenameToo: true);
                    clsShared.cSubs.NormalMouse();
                    if (flag3)
                    {
                        Interaction.MsgBox(
                            "This project is already open in another copy of yWriter.\r\nIf you don't think this is the case, please close ALL instances of yWriter and try again. (If you are certain, manually delete the LOCK file in the project folder before reloading your project.)",
                            MsgBoxStyle.Exclamation, "Unable to load " + ProjectFilename);
                    }
                    else
                    {
                        HideSplashEvent?.Invoke();
                        if (!flag2)
                        {
                            clsShared.cShowError.ShowErr(
                                "There was an error loading this project. Please submit an error report - and if possible, email your YW6 project file to spacejock @ gmail.com",
                                MethodBase.GetCurrentMethod(), "");
                        }
                    }
                }

                mvarLoading = false;
                clsShared.cSubs.NormalMouse();
                return flag;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
            }
        }

        public bool LoadProjectFromXML(string XMLData)
        {
            bool flag = true;
            string text = "";
            try
            {
                Clear(FilenameToo: false);
                clsShared.cLog.AddAction("Extracting project from xml");
                text = cXml.ExtractWithTags(XMLData, "<PROJECT>", "</PROJECT>");
                if (!CreateFromXML(text))
                {
                    clsShared.cLog.AddError("Load failed ... unable to extract project data from " + FileWithPath,
                        MethodBase.GetCurrentMethod());
                    flag = false;
                }

                if (flag && UsePassword)
                {
                    clsShared.cLog.AddAction("Passwords not yet implemented on yw6 files");
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Books");
                    text = cXml.ExtractWithTags(XMLData, "<PROJECTBOOKS>", "</PROJECTBOOKS>");
                    if (!cBooks.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Unable to extract book data from " + FileWithPath);
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Parts");
                    text = cXml.ExtractWithTags(XMLData, "<PROJECTPARTS>", "</PROJECTPARTS>");
                    if (!cParts.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Unable to extract book data from " + FileWithPath);
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Chapters");
                    text = cXml.ExtractWithTags(XMLData, "<CHAPTERS>", "</CHAPTERS>");
                    if (!cChapters.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Load failed ... unable to extract chapter data from " + FileWithPath);
                        flag = false;
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Scenes");
                    text = cXml.ExtractWithTags(XMLData, "<SCENES>", "</SCENES>");
                    if (!cScenes.CreateFromXMLV6(ProjectPath(), text))
                    {
                        clsShared.cLog.AddAction("Load failed ... unable to extract scene data / load scenes from " +
                                                 FileWithPath);
                        flag = false;
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Locations");
                    text = cXml.ExtractWithTags(XMLData, "<LOCATIONS>", "</LOCATIONS>");
                    if (!cLocations.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Load failed ... unable to extract location data from " +
                                                 FileWithPath);
                        flag = false;
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Characters");
                    text = cXml.ExtractWithTags(XMLData, "<CHARACTERS>", "</CHARACTERS>");
                    if (!cChars.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction(
                            "Load failed ... unable to extract character data from " + FileWithPath);
                        flag = false;
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Items");
                    text = cXml.ExtractWithTags(XMLData, "<ITEMS>", "</ITEMS>");
                    if (!cItems.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Load failed ... unable to extract items data from " + FileWithPath);
                        flag = false;
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Notes");
                    text = cXml.ExtractWithTags(XMLData, "<PROJECTNOTES>", "</PROJECTNOTES>");
                    if (!cNotes.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Unable to extract notes data from " + FileWithPath);
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Variables");
                    text = cXml.ExtractWithTags(XMLData, "<PROJECTVARS>", "</PROJECTVARS>");
                    if (!cVars.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Unable to extract variables from " + FileWithPath);
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Custom Exports");
                    text = cXml.ExtractWithTags(XMLData, "<PROJECTEXPS>", "</PROJECTEXPS>");
                    if (!cExports.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Unable to extract Custom Exports from " + FileWithPath);
                    }
                }

                if (flag)
                {
                    ProgressEvent?.Invoke("Loading Wordcount Logs");
                    text = cXml.ExtractWithTags(XMLData, "<WCLog>", "</WCLog>");
                    if (!mvarWordLogs.CreateFromXML(text))
                    {
                        clsShared.cLog.AddAction("Unable to extract wordcount log data from " + FileWithPath);
                    }
                }

                if (flag)
                {
                    IEnumerator enumerator = default(IEnumerator);
                    try
                    {
                        enumerator = Chapters.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            clsChapter clsChapter = (clsChapter) enumerator.Current;
                            int sceneCount = clsChapter.SceneCount;
                            for (int i = 1; i <= sceneCount; i++)
                            {
                                clsScene clsScene = this.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
                                if (clsScene.BelongsToChID != clsChapter.ID)
                                {
                                    clsShared.cLog.AddAction("Scene ID " + Conversions.ToString(clsScene.ID) +
                                                             " says it belongs to chapter ID " +
                                                             Conversions.ToString(clsScene.BelongsToChID) +
                                                             " but should be " + Conversions.ToString(clsChapter.ID) +
                                                             ". Fixed");
                                    clsScene.BelongsToChID = clsChapter.ID;
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator is IDisposable)
                        {
                            (enumerator as IDisposable).Dispose();
                        }
                    }

                    ProgressEvent?.Invoke("Loading Complete");
                    if (!ProjectIsSane())
                    {
                        clsShared.cShowError.ShowErr("Indexing error in project file", MethodBase.GetCurrentMethod(),
                            "");
                    }
                    else
                    {
                        MakeSubFolders();
                        ProgressEvent?.Invoke("Creating autobackups");
                        BackupProject(WithSceneContent: false);
                    }
                }

                if (!flag)
                {
                    clsShared.cLog.AddAction("Project extraction failed");
                    return flag;
                }

                clsShared.cLog.AddAction("Project extracted");
                return flag;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
            }
        }

        public bool CriticalSave()
        {
            return Save(CriticalSave: true);
        }

        public bool QuickSave()
        {
            return Save(CriticalSave: false);
        }

        private bool Save(bool CriticalSave)
        {
            if (mvarV6)
            {
                if (Operators.CompareString(mvarXMLFilename, "", TextCompare: false) == 0)
                {
                    return SaveProject(CriticalSave, EmbedRTF: false, V6File: true);
                }

                mvarXMLFilename = clsShared.cFile.FileWithoutExt(mvarXMLFilename) + ".yw6";
                return SaveProject(CriticalSave, EmbedRTF: false, V6File: true);
            }

            if (Operators.CompareString(mvarXMLFilename, "", TextCompare: false) == 0)
            {
                return SaveProject(CriticalSave, EmbedRTF: false, V6File: false);
            }

            mvarXMLFilename = clsShared.cFile.FileWithoutExt(mvarXMLFilename) + ".yw5";
            return SaveProject(CriticalSave, EmbedRTF: false, V6File: false);
        }

        public bool QuickSaveYWCompacted5()
        {
            mvarXMLFilename = clsShared.cFile.FileWithoutExt(mvarXMLFilename) + ".ywc5";
            return SaveProject(CriticalSave: true, EmbedRTF: true, V6File: false);
        }

        public bool FullSaveYWCompacted5()
        {
            mvarXMLFilename = clsShared.cFile.FileWithoutExt(mvarXMLFilename) + ".ywc5";
            return SaveProject(CriticalSave: true, EmbedRTF: true, V6File: false);
        }

        private bool SaveProject(bool CriticalSave, bool EmbedRTF, bool V6File)
        {
            bool result;
            if (!CriticalSave)
            {
                colSaveTokens.Add(strSaveToken);
                result = true;
            }
            else if (mvarSaving)
            {
                clsShared.cLog.AddAction("Project save cancelled - another save is already in progress",
                    MethodBase.GetCurrentMethod());
                result = false;
            }
            else
            {
                mvarSaving = true;
                SaveTimer.Stop();
                string text = "";
                string text2 = clsShared.cSpecialFolders.MyDocsFolder;
                if (!clsShared.cFile.FolderExists(text2))
                {
                    text2 = "C:\\yWriter Project\\";
                    clsShared.cFile.MkMissingDir(text2);
                }

                UpdateWCLog();
                ZipCurrentProject();
                try
                {
                    if (!cProjectIO.CopyCurrentProjectFile(this))
                    {
                        clsShared.cLog.AddAction("Unable to rename current project to a .bak file");
                    }
                }
                catch (Exception ex)
                {
                    ProjectData.SetProjectError(ex);
                    Exception ex2 = ex;
                    ProjectData.ClearProjectError();
                }

                bool flag2 = default(bool);
                while (true)
                {
                    try
                    {
                        bool flag = false;
                        if (Operators.CompareString(FileWithPath, "", TextCompare: false) != 0)
                        {
                            goto IL_01a0;
                        }

                        if (V6File)
                        {
                            FileWithPath = clsShared.cFileSel.GetOutputFilename("Specify your new yWriter project file",
                                text2, "MyProject.yw6", "*.yw6", ForbidExisting: true);
                        }
                        else
                        {
                            FileWithPath = clsShared.cFileSel.GetOutputFilename("Specify your new yWriter project file",
                                text2, "MyProject.yw5", "*.yw5", ForbidExisting: true);
                        }

                        flag = true;
                        if (Operators.CompareString(FileWithPath, "", TextCompare: false) != 0)
                        {
                            mvarXMLFilename = FileWithPath;
                            goto IL_01a0;
                        }

                        string text3 = "No project file specified. Save will be cancelled. Are you sure?";
                        if (flag2)
                        {
                            text3 =
                                "No project file specified. Save will be cancelled and YOU WILL LOSE DATA. Are you sure?";
                        }

                        clsShared.cLog.AddAction(text3, MethodBase.GetCurrentMethod());
                        if (Interaction.MsgBox(text3, MsgBoxStyle.YesNo) == MsgBoxResult.Yes)
                        {
                            clsShared.cLog.AddAction("User cancelled save. Project NOT saved.",
                                MethodBase.GetCurrentMethod());
                            mvarSaving = false;
                            return false;
                        }

                        goto end_IL_00cd;
                        IL_01a0:
                        if (!EmbedRTF && !V6File && flag &&
                            clsShared.cFile.FolderExists(modSubs.RTFPathV5(ProjectPath())))
                        {
                            text3 = "There's already a project in that location. Please choose a different folder.";
                            clsShared.cLog.AddAction(text3, MethodBase.GetCurrentMethod());
                            Interaction.MsgBox(text3);
                            goto IL_05b7;
                        }

                        clsShared.cFile.MkMissingDir(ProjectPath());
                        if (!EmbedRTF && !V6File)
                        {
                            clsShared.cFile.MkMissingDir(modSubs.RTFPathV5(ProjectPath()));
                            if (!clsShared.cFile.FolderExists(modSubs.RTFPathV5(ProjectPath())))
                            {
                                text3 =
                                    "Unable to create the project RTF folder. You must save to a different location or you will lose your work.";
                                clsShared.cShowError.ShowErr(text3, MethodBase.GetCurrentMethod(), "");
                                goto IL_05b7;
                            }
                        }

                        MakeSubFolders();
                        string filename = Path.Combine(ProjectPath(), "TestWrite.dat");
                        if (!clsShared.cFile.WriteTextFile(filename, "Test data"))
                        {
                            text3 = "The folder you have chosen appears to be read-only. Please specify a new folder.";
                            clsShared.cLog.AddAction(text3, MethodBase.GetCurrentMethod());
                            Interaction.MsgBox(text3);
                            goto IL_05b7;
                        }

                        clsShared.cFile.EraseFile(filename);
                        ProgressEvent?.Invoke("Saving project");
                        clsShared.cLog.AddAction("Saving project to " + FileWithPath);
                        clsShared.cLog.AddAction("Disk type is: " + clsShared.cFile.DriveTypeString(FileWithPath));
                        if (clsShared.cFile.DriveType(FileWithPath) != DriveType.Fixed)
                        {
                            clsShared.cLog.AddAction("Project folder is NOT stored on a fixed disk");
                        }

                        if (!V6File)
                        {
                            ProgressEvent?.Invoke("Saving Scene files");
                            if (!SaveSceneRTFFilesV5() && Interaction.MsgBox(
                                    "Failed to save one or more scene content files.\r\nDo you want to save your project to a different location?\r\n(I recommend YES or you may lose your work)",
                                    MsgBoxStyle.YesNo | MsgBoxStyle.Critical | MsgBoxStyle.Question) ==
                                MsgBoxResult.Yes)
                            {
                                goto IL_05b7;
                            }
                        }

                        DateTime now = DateTime.Now;
                        clsShared.cLog.AddAction("Packing project into XML data");
                        text = EntireProjectAsXML(V6File, EmbedRTF);
                        try
                        {
                            clsShared.cLog.AddAction(
                                "Took " + Conversions.ToString(DateTime.Now.Subtract(now).TotalMilliseconds) + " ms");
                        }
                        catch (Exception ex3)
                        {
                            ProjectData.SetProjectError(ex3);
                            Exception ex4 = ex3;
                            ProjectData.ClearProjectError();
                        }

                        bool flag3 = false;
                        if (V6File)
                        {
                            flag3 = clsShared.cFile.WriteUnicodeForced(FileWithPath, text);
                            if (!flag3)
                            {
                                clsShared.cLog.AddAction(
                                    "Project file in use while trying to save. Waiting 3 seconds.");
                                cDelay.Sleep(3000);
                                flag3 = clsShared.cFile.WriteUnicodeForced(FileWithPath, text);
                                if (!flag3)
                                {
                                    clsShared.cLog.AddAction(
                                        "Project file STILL in use while trying to save. Displaying dialog to user.");
                                }
                            }
                        }
                        else
                        {
                            flag3 = clsShared.cFile.WriteTextFile(FileWithPath, text, UseSystemEncoding: true);
                            if (flag3)
                            {
                                clsShared.cLog.AddAction("Project saved: " + FileWithPath + " with encoding " +
                                                         Encoding.Default.ToString());
                            }
                            else
                            {
                                clsShared.cLog.AddAction("Project was NOT saved.");
                            }
                        }

                        if (flag3)
                        {
                            clsShared.cLog.AddAction("Project save complete", MethodBase.GetCurrentMethod());
                            result = true;
                            mvarSaving = false;
                            modSaveLog.WriteProgressLog(this);
                            clsShared.cSettings.sSet("LastProjectFile", (object) FileWithPath);
                            clsShared.cSettings.sSet("LastProjectLoaded", (object) FileWithPath);
                            break;
                        }

                        if (new frmMsgBoxCustom(DialogResult.Retry, DialogResult.Yes, "Retry", "Save Elsewhere",
                                "Unable to save your project", "What would you like to do?",
                                "There was an error saving your project").ShowDialog() != DialogResult.Retry)
                        {
                            Interaction.MsgBox("Please choose a new location to save your project");
                            goto IL_05b7;
                        }

                        end_IL_00cd: ;
                    }
                    catch (Exception ex5)
                    {
                        ProjectData.SetProjectError(ex5);
                        Exception ex6 = ex5;
                        clsShared.cLog.AddAction(ex6, MethodBase.GetCurrentMethod());
                        result = false;
                        mvarSaving = false;
                        ProjectData.ClearProjectError();
                        break;
                    }

                    continue;
                    IL_05b7:
                    text2 = ProjectPath();
                    FileWithPath = "";
                    mvarXMLFilename = "";
                    flag2 = true;
                }

                clsShared.cLog.WriteActionsToLog();
                colSaveTokens.Clear();
                SaveTimer.Start();
            }

            return result;
        }

        private bool SaveSceneRTFFilesV5()
        {
            bool result = true;
            try
            {
                clsShared.cLog.AddAction("Saving scene RTF files to " + modSubs.RTFPathV5(ProjectPath()));
                if (cScenes.SaveScenesTextV5(ProjectPath()))
                {
                    return result;
                }

                bool flag = false;
                int num = 1;
                do
                {
                    clsShared.cLog.AddAction(
                        "Failed to save one or more scene content files. Retry #" + Conversions.ToString(num),
                        MethodBase.GetCurrentMethod());
                    cDelay.Sleep(1000);
                    if (cScenes.SaveScenesTextV5(ProjectPath()))
                    {
                        flag = true;
                        clsShared.cLog.AddAction(
                            "Retry successful. Scenes saved after " + Conversions.ToString(num) + " attempt(s)",
                            MethodBase.GetCurrentMethod());
                        break;
                    }

                    num++;
                } while (num <= 5);

                if (flag)
                {
                    return result;
                }

                result = false;
                if (!cScenes.SaveScenesTextV5(ProjectPath()))
                {
                    clsShared.cLog.AddAction(
                        "Failed to save one or more scene content files. That was the last attempt.");
                    StringBuilder SBP = new StringBuilder();
                    string text = Path.Combine(TodaysBackupFolder(), "Emergency Scene Backup.xml");
                    clsShared.cFile.MkMissingDir(TodaysBackupFolder());
                    if (Scenes.WriteXML(ref SBP, EmbedRTF: true, V6File: false) &&
                        !clsShared.cFile.WriteFileSysEncoded(text, SBP.ToString()))
                    {
                        text = Path.Combine(MyProject.Computer.FileSystem.SpecialDirectories.MyDocuments,
                            "Emergency Scene Backup.xml");
                        if (!clsShared.cFile.WriteFileSysEncoded(text, SBP.ToString()))
                        {
                            text = "";
                        }
                    }

                    SBP = null;
                    if (Operators.CompareString(text, "", TextCompare: false) == 0)
                    {
                        return result;
                    }

                    Interaction.MsgBox(
                        "Repeated attempts to save your scene data have failed. Therefore, the contents have been written to an emergency backup file:\r\n" +
                        text);
                    return result;
                }

                clsShared.cLog.AddAction(
                    "Saved with ForceRTF off. One or more of the scene files may have been in use.");
                return result;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public string EntireProjectAsXML(bool V6File, bool EmbedRTF)
        {
            StringBuilder SBP = new StringBuilder();
            try
            {
                if (V6File)
                {
                    SBP.AppendLine(cXml.XMLHeaderV6);
                    SBP.AppendLine("<YWRITER6>");
                }
                else
                {
                    SBP.AppendLine(modSubs.XMLHeaderV5);
                    SBP.AppendLine("<YWRITER5>");
                }

                SBP.AppendLine(DataAsXML());
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }

            if (cChapters.Count == 0)
            {
                clsShared.cLog.AddAction("No chapters in project");
            }

            try
            {
                if (!cLocations.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing locations into project");
                }

                if (!cItems.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing items into project");
                }

                if (!cChars.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing characters into project");
                }

                if (!cNotes.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing notes into project");
                }

                if (!cVars.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing vars into project");
                }

                if (!cExports.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing exports into project");
                }

                if (!cBooks.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing books into project");
                }

                if (!cParts.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing parts into project");
                }
            }
            catch (Exception ex3)
            {
                ProjectData.SetProjectError(ex3);
                Exception ex4 = ex3;
                clsShared.cLog.AddAction(ex4, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }

            if (V6File)
            {
                clsShared.cLog.AddAction("V6File is true - Saving scene text inside the project file");
            }
            else if (EmbedRTF)
            {
                clsShared.cLog.AddAction("EmbedRTF is true - Saving scene RTFs inside the project file");
            }

            try
            {
                if (!cScenes.WriteXML(ref SBP, EmbedRTF, V6File))
                {
                    clsShared.cLog.AddAction("Error packing scenes into project");
                }
            }
            catch (Exception ex5)
            {
                ProjectData.SetProjectError(ex5);
                Exception ex6 = ex5;
                clsShared.cLog.AddAction(ex6, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }

            try
            {
                if (!cChapters.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing chapters into project");
                }
            }
            catch (Exception ex7)
            {
                ProjectData.SetProjectError(ex7);
                Exception ex8 = ex7;
                clsShared.cLog.AddAction(ex8, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }

            try
            {
                if (!mvarWordLogs.WriteXML(ref SBP))
                {
                    clsShared.cLog.AddAction("Error packing word logs into project");
                }
            }
            catch (Exception ex9)
            {
                ProjectData.SetProjectError(ex9);
                Exception ex10 = ex9;
                clsShared.cLog.AddAction(ex10, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }

            try
            {
                if (V6File)
                {
                    SBP.AppendLine("</YWRITER6>");
                }
                else
                {
                    SBP.AppendLine("</YWRITER5>");
                }
            }
            catch (Exception ex11)
            {
                ProjectData.SetProjectError(ex11);
                Exception ex12 = ex11;
                clsShared.cLog.AddAction(ex12, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }

            return SBP.ToString();
        }

        public void AddChar(ref clsCharacter cChr)
        {
            try
            {
                cChars.AddChar(ref cChr);
                AddedNewCharEvent?.Invoke(cChr.ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public int AddNewChar()
        {
            clsCharacter cChr = new clsCharacter();
            cChr.Title = "New Character";
            AddChar(ref cChr);
            return cChr.ID;
        }

        public int AddNewScene(int BelongsToChID)
        {
            clsScene Scene = new clsScene();
            Scene.Title = "New Scene";
            AddSceneToProjectAndChapter(ref Scene, BelongsToChID);
            return Scene.ID;
        }

        public int AddNewScene(int BelongsToChID, int BeforeSceneIndex)
        {
            clsScene Scene = new clsScene();
            Scene.Title = "New Scene";
            AddSceneToProjectAndChapter(ref Scene, BelongsToChID, BeforeSceneIndex);
            return Scene.ID;
        }

        public int AddNewChapter()
        {
            int num = 0;
            int num2 = 0;
            int num3 = 0;
            for (int i = ChapterCount; i >= 1; i += -1)
            {
                try
                {
                    num = Conversions.ToInteger(Strings.Replace(this.get_ChapterByIndex(i).Title, "Chapter", ""));
                }
                catch (Exception ex)
                {
                    ProjectData.SetProjectError(ex);
                    Exception ex2 = ex;
                    ProjectData.ClearProjectError();
                }

                if (num > num2)
                {
                    num2 = num;
                }
            }

            num3 = num2 + 1;
            clsChapter cChapter = new clsChapter();
            cChapter.Title = "Chapter " + Conversions.ToString(num3);
            AddChapter(ref cChapter);
            return cChapter.ID;
        }

        public int AddNewItem()
        {
            clsItem Item = new clsItem();
            Item.Title = "New Item";
            AddItem(ref Item);
            return Item.ID;
        }

        public int AddNewLocation()
        {
            clsLocation Location = new clsLocation();
            Location.Title = "New Location";
            AddLocation(ref Location);
            return Location.ID;
        }

        public void AddChapter(ref clsChapter cChapter)
        {
            try
            {
                cChapters.AddChapter(ref cChapter);
                AddedNewChapterEvent?.Invoke(cChapter.ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void Clear(bool FilenameToo)
        {
            if (FileWithPath.Length > 0)
            {
                cProjectIO.UnlockProject(FileWithPath);
            }

            if (FilenameToo)
            {
                mvarXMLFilename = "";
            }

            cChapters.Clear();
            cChars.Clear();
            cLocations.Clear();
            cScenes.Clear();
            cItems.Clear();
            cNotes.Clear();
            cVars.Clear();
            cParts.Clear();
            cBooks.Clear();
            ClearWordLogs();
            CreateFromXML("");
        }

        public void ClearWordLogs()
        {
            mvarWordLogs.Clear();
        }

        public void InsertCharacter(ref clsCharacter Character)
        {
            try
            {
                cChars.AddChar(ref Character);
                AddedNewCharEvent?.Invoke(Character.ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void InsertChapter(ref clsChapter Chapter, int Before)
        {
            try
            {
                cChapters.InsertChapter(ref Chapter, Before);
                AddedNewChapterEvent?.Invoke(Chapter.ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AddSceneToProjectAndChapter(ref clsScene Scene, int BelongsToChID)
        {
            try
            {
                Scene.BelongsToChID = BelongsToChID;
                Scene.V6 = mvarV6;
                cScenes.AddScene(ref Scene);
                this.get_ChapterByID(BelongsToChID).AddSceneID(Scene.ID);
                clsShared.cLog.AddAction("Added scene ID: " + Conversions.ToString(Scene.ID) + " Title: " +
                                         Scene.Title);
                AddedNewSceneEvent?.Invoke(Scene.ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AddSceneToProjectAndChapter(ref clsScene Scene, int BelongsToChID, int BeforeSceneIndex)
        {
            try
            {
                Scene.BelongsToChID = BelongsToChID;
                Scene.V6 = mvarV6;
                cScenes.AddScene(ref Scene);
                if (BeforeSceneIndex == 0)
                {
                    this.get_ChapterByID(BelongsToChID).AddSceneID(Scene.ID);
                }
                else
                {
                    this.get_ChapterByID(BelongsToChID).InsertSceneIDBeforeIndex(Scene.ID, BeforeSceneIndex);
                }

                AddedNewSceneEvent?.Invoke(Scene.ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AddLocation(ref clsLocation Location)
        {
            try
            {
                cLocations.AddLocation(ref Location);
                AddedNewLocationEvent?.Invoke(Location.ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AddItem(ref clsItem Item)
        {
            try
            {
                cItems.AddItem(ref Item);
                AddedNewItemEvent?.Invoke(Item.ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AddNote(ref clsNote cNote)
        {
            try
            {
                cNotes.AddNote(ref cNote);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AddBook(ref clsBook cBook)
        {
            try
            {
                cBooks.AddBook(ref cBook);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AddPart(ref clsPart cPart)
        {
            try
            {
                cParts.AddPart(ref cPart);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        private void cNotes_SavedNote(int ID)
        {
            try
            {
                SavedNoteEvent?.Invoke(ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                ProjectData.ClearProjectError();
            }
        }

        private void cScenes_SavedScene(int ID)
        {
            try
            {
                SavedSceneEvent?.Invoke(ID);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                ProjectData.ClearProjectError();
            }
        }

        private void cScenes_LoadingNum(int Scenenum)
        {
            ProgressEvent?.Invoke("Loading Scene " + Conversions.ToString(Scenenum));
        }

        private void cScenes_SavingNum(int Scenenum)
        {
            ProgressEvent?.Invoke("Saving Scene " + Conversions.ToString(Scenenum));
        }

        public string DataAsXML()
        {
            StringBuilder stringBuilder = new StringBuilder();
            string str = "\t\t";
            string result = "";
            try
            {
                stringBuilder.AppendLine("\t<PROJECT>");
                string value = str + cXml.EncloseInt(5, "<Ver>", "</Ver>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseCData(mvarTITLE, "<Title>", "</Title>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseCData(mvarLatexHeaderFile, "<LaTeXHeaderFile>", "</LaTeXHeaderFile>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseCData(mvarDesc, "<Desc>", "</Desc>");
                stringBuilder.AppendLine(value);
                int num = 1;
                do
                {
                    value = str + cXml.EncloseCData(mvarFieldTitle[num],
                                "<FieldTitle" + Conversions.ToString(num) + ">",
                                "</FieldTitle" + Conversions.ToString(num) + ">");
                    stringBuilder.AppendLine(value);
                    num++;
                } while (num <= 4);

                value = str + cXml.EncloseCData(mvarBio, "<Bio>", "</Bio>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseCData(mvarAuthorname, "<AuthorName>", "</AuthorName>");
                stringBuilder.AppendLine(value);
                if (mvarUsePassword)
                {
                    value = str + cXml.EncloseBase64(mvarPassword, "<Password>", "</Password>");
                    stringBuilder.AppendLine(value);
                    value = str + cXml.EncloseBool(mvarUsePassword, "<UsePassword>", "</UsePassword>");
                    stringBuilder.AppendLine(value);
                }

                if (Collab)
                {
                    value = str + cXml.EncloseBool(mvarCollab, "Collab");
                    stringBuilder.AppendLine(value);
                }

                if (Collab)
                {
                    value = str + cXml.EncloseString(mvarCollabFolder, "CollabFolder");
                    stringBuilder.AppendLine(value);
                }

                value = str + cXml.EncloseInt(mvarWordTarget, "<WordTarget>", "</WordTarget>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseInt(mvarEndFrontMatterChID, "<EndFrontMatterChID>", "</EndFrontMatterChID>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseInt(mvarEndMiddleChID, "<EndMiddleChID>", "</EndMiddleChID>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseInt(mvarEndBeginningChID, "<EndBeginningChID>", "</EndBeginningChID>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseInt(mvarWordCountStart, "<WordCountStart>", "</WordCountStart>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseFloat(4.0, "<XMLFileVersion>", "</XMLFileVersion>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseBool(mvarLatexSingleQuotes, "<LatexSingleQuotes>", "</LatexSingleQuotes>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseBool(mvarLatexSuppressChapterTitles, "<LatexSuppressChapterTitles>",
                            "</LatexSuppressChapterTitles>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseInt(mvarLatexSceneDividerStyle, "LatexSceneDividerStyle");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseString(mvarHtmlSceneDivider, "HtmlSceneDivider");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseString(mvarHTMLEncoding, "HtmlEncoding");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseBool(mvarHTMLUnicode, "<HtmlUnicode>", "</HtmlUnicode>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseDate(mvarLastFullBackup.Date, "<LastFullBackup>", "</LastFullBackup>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseDate(mvarWordTargetStartDate, "<WordTargetStartDate>",
                            "</WordTargetStartDate>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseDate(mvarWordTargetEndDate, "<WordTargetEndDate>", "</WordTargetEndDate>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseDate(mvarMRUSpecificDate, "<MRUSpecificDate>", "</MRUSpecificDate>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseTime(mvarMRUSpecificTime, "<MRUSpecificTime>", "</MRUSpecificTime>");
                stringBuilder.AppendLine(value);
                value = str + cXml.EncloseInt(mvarLastViewedScID, "<LastViewedScID>", "</LastViewedScID>");
                stringBuilder.AppendLine(value);
                num = 1;
                do
                {
                    value = str + cXml.EncloseDate(mvarDeadlineDate[num], "DeadlineDate" + Conversions.ToString(num));
                    stringBuilder.AppendLine(value);
                    num++;
                } while (num <= 5);

                num = 1;
                do
                {
                    value = str + cXml.EncloseDate(mvarDeadlineStartDate[num],
                                "DeadlineStartDate" + Conversions.ToString(num));
                    stringBuilder.AppendLine(value);
                    num++;
                } while (num <= 5);

                stringBuilder.AppendLine("\t</PROJECT>");
                result = stringBuilder.ToString();
                return result;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public bool CreateFromXML(string XMLData)
        {
            bool result = true;
            try
            {
                UpdateFromXML(XMLData);
                mvarLastViewedScID = cXml.ExtractInt(XMLData, "LastViewedScID");
                mvarUsePassword = cXml.ExtractBool(XMLData, "UsePassword");
                if (mvarUsePassword)
                {
                    mvarPassword = cXml.ExtractBase64(XMLData, "<Password>", "</Password>");
                }

                mvarCollab = cXml.ExtractBool(XMLData, "Collab");
                mvarCollabFolder = cXml.ExtractString(XMLData, "CollabFolder");
                mvarLastFullBackup = cXml.ExtractDate(XMLData, "LastFullBackup");
                mvarMRUSpecificDate = cXml.ExtractDate(XMLData, "<MRUSpecificDate>", "</MRUSpecificDate>");
                mvarMRUSpecificTime = cXml.ExtractTime(XMLData, "<MRUSpecificTime>", "</MRUSpecificTime>");
                mvarLatexHeaderFile = cXml.ExtractCData(XMLData, "<LaTeXHeaderFile>", "</LaTeXHeaderFile>");
                mvarLatexSingleQuotes = cXml.ExtractBool(XMLData, "LatexSingleQuotes");
                mvarLatexSuppressChapterTitles = cXml.ExtractBool(XMLData, "LatexSuppressChapterTitles");
                mvarLatexSceneDividerStyle = cXml.ExtractInt(XMLData, "LatexSceneDividerStyle");
                mvarHtmlSceneDivider = cXml.ExtractString(XMLData, "HtmlSceneDivider");
                mvarHTMLEncoding = cXml.ExtractString(XMLData, "HtmlEncoding");
                mvarHTMLUnicode = cXml.ExtractBool(XMLData, "HtmlUnicode");
                return result;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                result = false;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public bool UpdateFromXML(string XMLData)
        {
            bool result = true;
            try
            {
                _ = new string[2];
                int num;
                if (cXml.ExtractInt(XMLData, "<Ver>", "</Ver>") >= 5)
                {
                    mvarTITLE = cXml.ExtractCData(XMLData, "<Title>", "</Title>");
                    mvarDesc = cXml.ExtractCData(XMLData, "<Desc>", "</Desc>");
                    mvarBio = cXml.ExtractCData(XMLData, "<Bio>", "</Bio>");
                    mvarAuthorname = cXml.ExtractCData(XMLData, "<AuthorName>", "</AuthorName>");
                    num = 1;
                    do
                    {
                        mvarFieldTitle[num] = cXml.ExtractCData(XMLData,
                            "<FieldTitle" + Conversions.ToString(num) + ">",
                            "</FieldTitle" + Conversions.ToString(num) + ">");
                        num++;
                    } while (num <= 4);
                }
                else
                {
                    mvarTITLE = cXml.ExtractString(XMLData, "<Title>", "</Title>");
                    mvarDesc = cXml.ExtractString(XMLData, "<Desc>", "</Desc>");
                    mvarBio = cXml.ExtractString(XMLData, "<Bio>", "</Bio>");
                    mvarAuthorname = cXml.ExtractString(XMLData, "<AuthorName>", "</AuthorName>");
                    num = 1;
                    do
                    {
                        mvarFieldTitle[num] = cXml.ExtractString(XMLData,
                            "<FieldTitle" + Conversions.ToString(num) + ">",
                            "</FieldTitle" + Conversions.ToString(num) + ">");
                        num++;
                    } while (num <= 4);
                }

                mvarXMLFileVersion = cXml.ExtractInt(XMLData, "<XMLFileVersion>", "</XMLFileVersion>");
                mvarWordTarget = cXml.ExtractInt(XMLData, "<WordTarget>", "</WordTarget>");
                mvarWordCountStart = cXml.ExtractInt(XMLData, "<WordCountStart>", "</WordCountStart>");
                mvarWordTargetStartDate = cXml.ExtractDate(XMLData, "<WordTargetStartDate>", "</WordTargetStartDate>");
                mvarWordTargetEndDate = cXml.ExtractDate(XMLData, "<WordTargetEndDate>", "</WordTargetEndDate>");
                mvarEndMiddleChID = cXml.ExtractInt(XMLData, "<EndMiddleChID>", "</EndMiddleChID>");
                mvarEndBeginningChID = cXml.ExtractInt(XMLData, "<EndBeginningChID>", "</EndBeginningChID>");
                mvarEndFrontMatterChID = cXml.ExtractInt(XMLData, "<EndFrontMatterChID>", "</EndFrontMatterChID>");
                num = 1;
                do
                {
                    mvarDeadlineDate[num] = cXml.ExtractDate(XMLData, "<DeadlineDate" + Conversions.ToString(num) + ">",
                        "</DeadlineDate" + Conversions.ToString(num) + ">");
                    mvarDeadlineStartDate[num] = cXml.ExtractDate(XMLData,
                        "<DeadlineStartDate" + Conversions.ToString(num) + ">",
                        "</DeadlineStartDate" + Conversions.ToString(num) + ">");
                    num++;
                } while (num <= 5);

                return result;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                result = false;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public void MakeImagesFolder()
        {
            try
            {
                clsShared.cFile.MkMissingDir(ImagesFolder);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void MakeAutoBackupFolder()
        {
            try
            {
                clsShared.cFile.MkMissingDir(AutobackupsFolder);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public string TodaysBackupFolder()
        {
            string result = "";
            try
            {
                result = Path.Combine(AutobackupsFolder, modDateTime.DateToStringNoTime(DateTime.Now.Date));
                return result;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public string TodaysAltBackupFolder()
        {
            string result = "";
            try
            {
                result = Path.Combine(clsShared.cSettings.sGet("AltBackupFolder_" + App.ComputerName, ""),
                    Strings.Format(DateTime.Now.Date, "yyyy-MM-dd"));
                return result;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public bool BackupThisFile(string Filename, string Zipfile)
        {
            bool flag = default(bool);
            try
            {
                clsZip clsZip = new clsZip();
                if (clsShared.cFile.FileExists(Zipfile))
                {
                    if (clsZip.AddFileToZip(Zipfile, Filename))
                    {
                        flag = true;
                    }
                }
                else if (clsZip.Zip(Zipfile, Filename))
                {
                    flag = true;
                }

                if (flag)
                {
                    return flag;
                }

                clsShared.cLog.AddAction("Error making backup of " + Filename);
                return flag;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return flag;
            }
        }

        public string TodaysBackupFilename(bool WithSceneContent)
        {
            return BackupFilename(WithSceneContent, TodaysBackupFolder());
        }

        public string BackupFilename(bool WithSceneContent, string Destfolder)
        {
            if (WithSceneContent)
            {
                return Path.Combine(Destfolder,
                    "Full Backup of " + clsShared.cFile.FileWithoutExt(clsShared.cFile.FileOnly(mvarXMLFilename)) +
                    " " + Strings.Format(DateTime.Now, "yyyy-MM-dd") + ".zip");
            }

            return Path.Combine(Destfolder,
                "Partial Backup of " + clsShared.cFile.FileWithoutExt(clsShared.cFile.FileOnly(mvarXMLFilename)) + " " +
                Strings.Format(DateTime.Now, "yyyy-MM-dd") + ".zip");
        }

        public bool BackupProject(bool WithSceneContent)
        {
            string destfolder = TodaysBackupFolder();
            string destFile = BackupFilename(WithSceneContent, destfolder);
            return cProjectIO.BackupProject(this, WithSceneContent, destfolder, destFile);
        }

        public bool BackupProjectToAltfolder(bool WithSceneContent)
        {
            if (Operators.CompareString(clsShared.cSettings.sGet("AltBackupFolder_" + App.ComputerName, ""), "",
                    TextCompare: false) == 0)
            {
                clsShared.cLog.AddAction(
                    "Cannot perform Alternate backup, as the Alt Backup Folder has not been specified");
                bool result = default(bool);
                return result;
            }

            string destfolder = clsShared.cSettings.sGet("AltBackupFolder_" + App.ComputerName, "");
            string destFile = BackupFilename(WithSceneContent, destfolder);
            return cProjectIO.BackupProject(this, WithSceneContent, destfolder, destFile);
        }

        public int VPUsedCount(int CharID)
        {
            int num = default(int);
            try
            {
                int sceneCount = SceneCount;
                for (int i = 1; i <= sceneCount; i++)
                {
                    if (this.get_SceneByIndex(i).ViewPointID == CharID)
                    {
                        num++;
                    }
                }

                return num;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return num;
            }
        }

        public bool IsViewpointSomewhere(int CharID)
        {
            bool result = default(bool);
            try
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = Scenes.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        if (((clsScene) enumerator.Current).ViewPointID == CharID)
                        {
                            result = true;
                            return result;
                        }
                    }

                    return result;
                }
                finally
                {
                    if (enumerator is IDisposable disposable)
                    {
                        disposable.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        private bool ProjectIsSane()
        {
            bool result = default(bool);
            try
            {
                result = true;
                int count = cChapters.Count;
                for (int i = 1; i <= count; i++)
                {
                    int sceneCount = this.get_ChapterByIndex(i).SceneCount;
                    for (int j = 1; j <= sceneCount; j++)
                    {
                        int num = this.get_ChapterByIndex(i).get_SceneIDByIndex(j);
                        if (this.get_SceneByID(num).ID == 0)
                        {
                            clsShared.cLog.AddAction("Scene ID " + Conversions.ToString(num) + " is referenced by '" +
                                                     this.get_ChapterByIndex(i).Title +
                                                     "', but doesn't exist in the project");
                            result = false;
                        }
                    }
                }

                int count2 = cScenes.Count;
                for (int i = 1; i <= count2; i++)
                {
                    for (int j = this.get_SceneByIndex(i).CharCount; j >= 1; j += -1)
                    {
                        int num = ((clsSceneBase) this.get_SceneByIndex(i)).get_CharID(j);
                        if (this.get_CharByID(num).ID == 0)
                        {
                            clsShared.cLog.AddAction("Character ID " + Conversions.ToString(num) +
                                                     " is referenced by '" + this.get_SceneByIndex(i).Title +
                                                     "', but doesn't exist in the project");
                            this.get_SceneByIndex(i).RemoveCharByID(num);
                        }
                    }
                }

                int count3 = cScenes.Count;
                for (int i = 1; i <= count3; i++)
                {
                    for (int j = this.get_SceneByIndex(i).LocationCount; j >= 1; j += -1)
                    {
                        int num = ((clsSceneBase) this.get_SceneByIndex(i)).get_LocationID(j);
                        if (this.get_LocationByID(num).ID == 0)
                        {
                            clsShared.cLog.AddAction("Location ID " + Conversions.ToString(num) +
                                                     " is referenced by '" + this.get_SceneByIndex(i).Title +
                                                     "', but doesn't exist in the project");
                            result = false;
                        }
                    }
                }

                int count4 = cScenes.Count;
                for (int i = 1; i <= count4; i++)
                {
                    for (int j = this.get_SceneByIndex(i).ItemCount; j >= 1; j += -1)
                    {
                        int num = ((clsSceneBase) this.get_SceneByIndex(i)).get_ItemID(j);
                        if (this.get_ItemByID(num).ID == 0)
                        {
                            clsShared.cLog.AddAction("Item ID " + Conversions.ToString(num) + " is referenced by '" +
                                                     this.get_SceneByIndex(i).Title +
                                                     "', but doesn't exist in the project");
                            result = false;
                        }
                    }
                }

                return result;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public string CharacterList(int ScID, string Separator)
        {
            string text = "";
            try
            {
                text = "";
                clsScene clsScene = this.get_SceneByID(ScID);
                if (clsScene.CharCount != 0)
                {
                    int num = clsScene.CharCount - 1;
                    for (int i = 1; i <= num; i++)
                    {
                        text = text + this.get_CharByID(((clsSceneBase) clsScene).get_CharID(i)).Title + Separator;
                    }

                    text += this.get_CharByID(((clsSceneBase) clsScene).get_CharID(clsScene.CharCount)).Title;
                    return text;
                }

                return text;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return text;
            }
        }

        public string ItemList(int ScID, string Separator)
        {
            string text = "";
            clsScene clsScene = this.get_SceneByID(ScID);
            if (clsScene.ItemCount != 0)
            {
                int num = clsScene.ItemCount - 1;
                for (int i = 1; i <= num; i++)
                {
                    text = text + this.get_ItemByID(((clsSceneBase) clsScene).get_ItemID(i)).Title + Separator;
                }

                text += this.get_ItemByID(((clsSceneBase) clsScene).get_ItemID(clsScene.ItemCount)).Title;
            }

            return text;
        }

        public string LocationList(int ScID, string Separator)
        {
            string text = "";
            clsScene clsScene = this.get_SceneByID(ScID);
            if (clsScene.LocationCount != 0)
            {
                int num = clsScene.LocationCount - 1;
                for (int i = 1; i <= num; i++)
                {
                    text = text + this.get_LocationByID(((clsSceneBase) clsScene).get_LocationID(i)).Title + Separator;
                }

                text += this.get_LocationByID(((clsSceneBase) clsScene).get_LocationID(clsScene.LocationCount)).Title;
            }

            return text;
        }

        public bool ChangeCharSortOrder(int SourceID, int DestID)
        {
            try
            {
                _ = this.get_CharByID(SourceID).SortOrder;
                int sortOrder = this.get_CharByID(DestID).SortOrder;
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = cChars.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsCharacter clsCharacter = (clsCharacter) enumerator.Current;
                        if (clsCharacter.SortOrder > this.get_CharByID(DestID).SortOrder)
                        {
                            clsCharacter.SortOrder++;
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }

                if (this.get_CharByID(SourceID).SortOrder < this.get_CharByID(DestID).SortOrder)
                {
                    this.get_CharByID(SourceID).SortOrder = sortOrder + 1;
                }
                else if (this.get_CharByID(SourceID).SortOrder > this.get_CharByID(DestID).SortOrder)
                {
                    this.get_CharByID(SourceID).SortOrder = sortOrder;
                    this.get_CharByID(DestID).SortOrder = sortOrder + 1;
                }

                return true;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                bool result = false;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public bool ChangeItemSortOrder(int SourceID, int DestID)
        {
            try
            {
                _ = this.get_ItemByID(SourceID).SortOrder;
                int sortOrder = this.get_ItemByID(DestID).SortOrder;
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = cItems.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsItem clsItem = (clsItem) enumerator.Current;
                        if (clsItem.SortOrder > this.get_ItemByID(DestID).SortOrder)
                        {
                            clsItem.SortOrder++;
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }

                if (this.get_ItemByID(SourceID).SortOrder < this.get_ItemByID(DestID).SortOrder)
                {
                    this.get_ItemByID(SourceID).SortOrder = sortOrder + 1;
                }
                else if (this.get_ItemByID(SourceID).SortOrder > this.get_ItemByID(DestID).SortOrder)
                {
                    this.get_ItemByID(SourceID).SortOrder = sortOrder;
                    this.get_ItemByID(DestID).SortOrder = sortOrder + 1;
                }

                return true;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                bool result = false;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public bool ChangeLocationSortOrder(int SourceID, int DestID)
        {
            try
            {
                int sortOrder = this.get_LocationByID(SourceID).SortOrder;
                int sortOrder2 = this.get_LocationByID(DestID).SortOrder;
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = cLocations.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsLocation clsLocation = (clsLocation) enumerator.Current;
                        if (clsLocation.SortOrder > sortOrder2)
                        {
                            clsLocation.SortOrder++;
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }

                if (sortOrder < sortOrder2)
                {
                    this.get_LocationByID(SourceID).SortOrder = sortOrder2 + 1;
                }
                else if (sortOrder > sortOrder2)
                {
                    this.get_LocationByID(SourceID).SortOrder = sortOrder2;
                    this.get_LocationByID(DestID).SortOrder = sortOrder2 + 1;
                }

                return true;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                bool result = false;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public void ChangeNoteSortOrder(int SourceID, int DestID)
        {
            try
            {
                _ = this.get_NoteByID(SourceID).SortOrder;
                int sortOrder = this.get_NoteByID(DestID).SortOrder;
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = cNotes.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsNote clsNote = (clsNote) enumerator.Current;
                        if (clsNote.SortOrder > this.get_NoteByID(DestID).SortOrder)
                        {
                            clsNote.SortOrder++;
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }

                if (this.get_NoteByID(SourceID).SortOrder < this.get_NoteByID(DestID).SortOrder)
                {
                    this.get_NoteByID(SourceID).SortOrder = sortOrder + 1;
                }
                else if (this.get_NoteByID(SourceID).SortOrder > this.get_NoteByID(DestID).SortOrder)
                {
                    this.get_NoteByID(SourceID).SortOrder = sortOrder;
                    this.get_NoteByID(DestID).SortOrder = sortOrder + 1;
                }
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void ChangeChapterSortOrder(int SourceID, int DestID)
        {
            try
            {
                clsChapter clsChapter = this.get_ChapterByID(DestID);
                clsChapter clsChapter2 = this.get_ChapterByID(SourceID);
                _ = clsChapter2.SortOrder;
                int sortOrder = clsChapter.SortOrder;
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = cChapters.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsChapter clsChapter3 = (clsChapter) enumerator.Current;
                        if (clsChapter3.SortOrder > sortOrder)
                        {
                            clsChapter3.SortOrder++;
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }

                if (clsChapter2.SortOrder < sortOrder)
                {
                    clsChapter2.SortOrder = sortOrder + 1;
                }
                else if (clsChapter2.SortOrder > clsChapter.SortOrder)
                {
                    clsChapter2.SortOrder = sortOrder;
                    clsChapter.SortOrder = sortOrder + 1;
                }
                else
                {
                    clsChapter2.SortOrder = sortOrder;
                    clsChapter.SortOrder = sortOrder + 1;
                }
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void ResortCharacters()
        {
            Chars.Resort();
        }

        public void ResortLocations()
        {
            Locations.Resort();
        }

        public void ResortItems()
        {
            Items.Resort();
        }

        public void ResortNotes()
        {
            Notes.Resort();
        }

        public void ResortChapters()
        {
            Chapters.Resort();
        }

        private void cChars_Saved(int ID)
        {
            SavedCharEvent?.Invoke(ID);
        }

        private void cChapters_Saved(int ID)
        {
            SavedChapterEvent?.Invoke(ID);
        }

        private void cItems_Saved(int ID)
        {
            SavedItemEvent?.Invoke(ID);
        }

        private void cLocations_Saved(int ID)
        {
            SavedLocationEvent?.Invoke(ID);
        }

        public int LatestWCDiff(bool WithUnused)
        {
            return mvarWordLogs.LatestDiff(WithUnused);
        }

        public void UpdateWCLog()
        {
            try
            {
                mvarWordLogs.UpdateCount(Wordcount(), WordcountWithUnused());
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        ~clsProject()
        {
        }

        public clsCollection<clsChapter> AllChapters()
        {
            return FilteredChapters(WithUnused: true, WithInfoType: true, -1, -1);
        }

        public clsCollection<clsChapter> AllChaptersUsedAllTypes()
        {
            return FilteredChapters(WithUnused: false, WithInfoType: true, -1, -1);
        }

        public clsCollection<clsChapter> AllChaptersUsedNoInfoType()
        {
            return FilteredChapters(WithUnused: false, WithInfoType: false, -1, -1);
        }

        public clsCollection<clsChapter> FilteredChapters(bool WithUnused, bool WithInfoType, int BookID, int PartID)
        {
            clsCollection<clsChapter> clsCollection = new clsCollection<clsChapter>();
            bool flag = (BookID > 0) ? true : false;
            bool flag2 = (PartID > 0) ? true : false;
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = Chapters.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    clsChapter clsChapter = (clsChapter) enumerator.Current;
                    if (((clsChapter.BookID == BookID) | !flag) && ((clsChapter.PartID == PartID) | !flag2) &&
                        (!clsChapter.Unused | WithUnused) &&
                        ((clsChapter.Type == ChapterTypes.ChapterNormal) | WithInfoType))
                    {
                        clsCollection.Add(clsChapter, Conversions.ToString(clsChapter.ID));
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            return clsCollection;
        }

        public clsCollection<int> SceneIDsInOrder(bool WithUnused)
        {
            return SceneIDsInOrder(Chapters.colObjects, WithUnused);
        }

        public clsCollection<int> SceneIDsInOrder(clsCollection<clsChapter> colChapters, bool WithUnused)
        {
            clsCollection<int> clsCollection = new clsCollection<int>();
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = colChapters.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    clsChapter clsChapter = (clsChapter) enumerator.Current;
                    if (WithUnused)
                    {
                        int sceneCount = clsChapter.SceneCount;
                        for (int i = 1; i <= sceneCount; i++)
                        {
                            int item = clsChapter.get_SceneIDByIndex(i);
                            clsCollection.Add(item);
                        }
                    }
                    else if (!clsChapter.Unused)
                    {
                        int sceneCount2 = clsChapter.SceneCount;
                        for (int j = 1; j <= sceneCount2; j++)
                        {
                            int item = clsChapter.get_SceneIDByIndex(j);
                            if (!this.get_SceneByID(item).Unused)
                            {
                                clsCollection.Add(item);
                            }
                        }
                    }
                }

                return clsCollection;
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        private void ZipCurrentProject()
        {
            try
            {
                cProjectIO.ZipCurrentProject(FileWithPath, TodaysBackupFolder());
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        private void MakeSubFolders()
        {
            try
            {
                clsShared.cFile.MkMissingDir(AutobackupsFolder);
                clsShared.cFile.MkMissingDir(TodaysBackupFolder());
                clsShared.cFile.MkMissingDir(ExportFolder());
                clsShared.cFile.MkMissingDir(LogsFolder);
                clsShared.cFile.MkMissingDir(ImagesFolder);
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public int RecalcProgressive()
        {
            int result = default(int);
            if (!AlreadyCalculating)
            {
                AlreadyCalculating= true;
                try
                {
                    int num = 0;
                    IEnumerator enumerator = default(IEnumerator);
                    try
                    {
                        enumerator = Chapters.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            clsChapter clsChapter = (clsChapter) enumerator.Current;
                            if (clsChapter.Type == ChapterTypes.ChapterNormal && !clsChapter.Unused)
                            {
                                num += this.get_ChapterWordcount(clsChapter, WithUnused: false);
                            }

                            clsChapter.ProgressiveCount = num;
                        }
                    }
                    finally
                    {
                        if (enumerator is IDisposable)
                        {
                            (enumerator as IDisposable).Dispose();
                        }
                    }

                    result = num;
                    UpdateWCLog();
                    AlreadyCalculating = false;
                    return result;
                }
                catch (Exception ex)
                {
                    ProjectData.SetProjectError(ex);
                    Exception ex2 = ex;
                    result = 0;
                    clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                    ProjectData.ClearProjectError();
                    return result;
                }
            }

            return result;
        }

        public bool CreateEmptyProject(bool V6Project)
        {
            Clear(FilenameToo: true);
            mvarV6 = V6Project;
            if (CriticalSave())
            {
                return true;
            }

            return false;
        }

        public void AutoAttachItems()
        {
            try
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = Chapters.GetEnumerator();
                    IEnumerator enumerator2 = default(IEnumerator);
                    while (enumerator.MoveNext())
                    {
                        clsChapter clsChapter = (clsChapter) enumerator.Current;
                        int sceneCount = clsChapter.SceneCount;
                        for (int i = 1; i <= sceneCount; i++)
                        {
                            clsScene clsScene = this.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
                            string plainText = clsScene.ConvertedToPlainText();
                            try
                            {
                                enumerator2 = Items.GetEnumerator();
                                while (enumerator2.MoveNext())
                                {
                                    clsItem clsItem = (clsItem) enumerator2.Current;
                                    if (!clsScene.ContainsItemID(clsItem.ID))
                                    {
                                        if (TextContainsTitle(plainText, clsItem.Title))
                                        {
                                            clsScene.AddItemByID(clsItem.ID);
                                        }
                                        else
                                        {
                                            string[] aKAArray = clsItem.AKAArray;
                                            foreach (string text in aKAArray)
                                            {
                                                if (Operators.CompareString(text, "", TextCompare: false) != 0 &&
                                                    TextContainsTitle(plainText, text))
                                                {
                                                    clsScene.AddItemByID(clsItem.ID);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 is IDisposable)
                                {
                                    (enumerator2 as IDisposable).Dispose();
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                clsShared.cShowError.ShowErr(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AutoAttachLocations()
        {
            try
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = Chapters.GetEnumerator();
                    IEnumerator enumerator2 = default(IEnumerator);
                    while (enumerator.MoveNext())
                    {
                        clsChapter clsChapter = (clsChapter) enumerator.Current;
                        int sceneCount = clsChapter.SceneCount;
                        for (int i = 1; i <= sceneCount; i++)
                        {
                            clsScene clsScene = this.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
                            string plainText = clsScene.ConvertedToPlainText();
                            try
                            {
                                enumerator2 = Locations.GetEnumerator();
                                while (enumerator2.MoveNext())
                                {
                                    clsLocation clsLocation = (clsLocation) enumerator2.Current;
                                    if (!clsScene.ContainsLocID(clsLocation.ID))
                                    {
                                        if (TextContainsTitle(plainText, clsLocation.Title))
                                        {
                                            clsScene.AddLocationByID(clsLocation.ID);
                                        }
                                        else
                                        {
                                            string[] aKAArray = clsLocation.AKAArray;
                                            foreach (string text in aKAArray)
                                            {
                                                if (Operators.CompareString(text, "", TextCompare: false) != 0 &&
                                                    TextContainsTitle(plainText, text))
                                                {
                                                    clsScene.AddLocationByID(clsLocation.ID);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 is IDisposable)
                                {
                                    (enumerator2 as IDisposable).Dispose();
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                clsShared.cShowError.ShowErr(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        public void AutoAttachCharacters()
        {
            try
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = Chapters.GetEnumerator();
                    IEnumerator enumerator2 = default(IEnumerator);
                    while (enumerator.MoveNext())
                    {
                        clsChapter clsChapter = (clsChapter) enumerator.Current;
                        int sceneCount = clsChapter.SceneCount;
                        for (int i = 1; i <= sceneCount; i++)
                        {
                            clsScene clsScene = this.get_SceneByID(clsChapter.get_SceneIDByIndex(i));
                            string plainText = clsScene.ConvertedToPlainText();
                            try
                            {
                                enumerator2 = Chars.GetEnumerator();
                                while (enumerator2.MoveNext())
                                {
                                    clsCharacter clsCharacter = (clsCharacter) enumerator2.Current;
                                    if (!clsScene.ContainsCharID(clsCharacter.ID))
                                    {
                                        if (TextContainsTitle(plainText, clsCharacter.Title))
                                        {
                                            clsScene.AddCharByID(clsCharacter.ID);
                                        }
                                        else if (TextContainsTitle(plainText, clsCharacter.FullName))
                                        {
                                            clsScene.AddCharByID(clsCharacter.ID);
                                        }
                                        else
                                        {
                                            string[] aKAArray = clsCharacter.AKAArray;
                                            foreach (string text in aKAArray)
                                            {
                                                if (Operators.CompareString(text, "", TextCompare: false) != 0 &&
                                                    TextContainsTitle(plainText, text))
                                                {
                                                    clsScene.AddCharByID(clsCharacter.ID);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 is IDisposable)
                                {
                                    (enumerator2 as IDisposable).Dispose();
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                clsShared.cShowError.ShowErr(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
            }
        }

        private bool TextContainsTitle(string PlainText, string Title)
        {
            bool result = false;
            if (Operators.CompareString(Title.Trim(), "", TextCompare: false) != 0)
            {
                if (PlainText.Contains(Title))
                {
                    result = true;
                }
                else if (Title.Contains(" ") && Strings.InStr(PlainText, Title, CompareMethod.Text) > 0)
                {
                    result = true;
                }
            }

            return result;
        }

        public bool AKAContains(ref string[] AKAArray, string StringToFind)
        {
            bool result = false;
            try
            {
                if (AKAArray != null)
                {
                    if (AKAArray.Length != 0)
                    {
                        string[] array = AKAArray;
                        for (int i = 0; i < array.Length; i = checked(i + 1))
                        {
                            if (Operators.CompareString(array[i], StringToFind, TextCompare: false) == 0)
                            {
                                result = true;
                                return result;
                            }
                        }

                        return result;
                    }

                    return result;
                }

                return result;
            }
            catch (Exception ex)
            {
                ProjectData.SetProjectError(ex);
                Exception ex2 = ex;
                clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
                ProjectData.ClearProjectError();
                return result;
            }
        }

        public clsCollection<string> SceneTags(bool WithUnused)
        {
            clsCollection<string> clsCollection = new clsCollection<string>();
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = Scenes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    clsScene clsScene = (clsScene) enumerator.Current;
                    if ((WithUnused | !this.get_ChapterByID(clsScene.BelongsToChID).Unused) &&
                        (WithUnused | !clsScene.Unused))
                    {
                        string[] splitTags = clsScene.SplitTags;
                        foreach (string text in splitTags)
                        {
                            if (Operators.CompareString(text.Trim(), "", TextCompare: false) != 0 &&
                                !clsCollection.Contains(text))
                            {
                                clsCollection.Add(text, text);
                            }
                        }
                    }
                }

                return clsCollection;
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public clsCollection<string> LocationTags()
        {
            clsCollection<string> clsCollection = new clsCollection<string>();
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = Locations.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string[] splitTags = ((clsLocation) enumerator.Current).SplitTags;
                    foreach (string text in splitTags)
                    {
                        if (Operators.CompareString(text.Trim(), "", TextCompare: false) != 0 &&
                            !clsCollection.Contains(text))
                        {
                            clsCollection.Add(text, text);
                        }
                    }
                }

                return clsCollection;
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public clsCollection<string> ItemTags()
        {
            clsCollection<string> clsCollection = new clsCollection<string>();
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = Items.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string[] splitTags = ((clsItem) enumerator.Current).SplitTags;
                    foreach (string text in splitTags)
                    {
                        if (Operators.CompareString(text.Trim(), "", TextCompare: false) != 0 &&
                            !clsCollection.Contains(text))
                        {
                            clsCollection.Add(text, text);
                        }
                    }
                }

                return clsCollection;
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public clsCollection<string> CharTags()
        {
            clsCollection<string> clsCollection = new clsCollection<string>();
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = Chars.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string[] splitTags = ((clsCharacter) enumerator.Current).SplitTags;
                    foreach (string text in splitTags)
                    {
                        if (Operators.CompareString(text.Trim(), "", TextCompare: false) != 0 &&
                            !clsCollection.Contains(text))
                        {
                            clsCollection.Add(text, text);
                        }
                    }
                }

                return clsCollection;
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public void SortCharAlpha()
        {
            cChars.AlphaSortMajorMinor();
            if (cChars.Count > 0)
            {
                SavedCharEvent?.Invoke(cChars.CharByIndex(1).ID);
            }
        }

        public bool AnyScenesLocked()
        {
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = Scenes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    clsScene clsScene = (clsScene) enumerator.Current;
                    if (clsScene.Locked | clsScene.EditingExternal)
                    {
                        return true;
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            return false;
        }

        public string PackageChapters(clsCollection<int> colChIDs)
        {
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder stringBuilder2 = new StringBuilder();
            clsCollection<int> clsCollection = new clsCollection<int>();
            stringBuilder2.AppendLine("<CHAPTERS>");
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = colChIDs.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    int iD = Conversions.ToInteger(enumerator.Current);
                    clsChapter clsChapter = this.get_ChapterByID(iD);
                    clsChapter.DataAsXML();
                    int sceneCount = clsChapter.SceneCount;
                    for (int i = 1; i <= sceneCount; i++)
                    {
                        clsCollection.Add(clsChapter.get_SceneIDByIndex(i));
                    }

                    stringBuilder2.AppendLine(clsChapter.DataAsXML());
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            stringBuilder2.AppendLine("</CHAPTERS>");
            stringBuilder.AppendLine(PackageScenes(clsCollection));
            stringBuilder.AppendLine(stringBuilder2.ToString());
            return stringBuilder.ToString();
        }

        public string PackageScenes(clsCollection<int> colScIDs)
        {
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder stringBuilder2 = new StringBuilder();
            string text = "";
            clsCollection<int> clsCollection = new clsCollection<int>();
            clsCollection<int> clsCollection2 = new clsCollection<int>();
            clsCollection<int> clsCollection3 = new clsCollection<int>();
            stringBuilder.AppendLine("<SCENES>");
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = colScIDs.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    int iD = Conversions.ToInteger(enumerator.Current);
                    clsScene clsScene = this.get_SceneByID(iD);
                    text = clsScene.DataAsXML(IncludingRTF: true);
                    int charCount = clsScene.CharCount;
                    for (int i = 1; i <= charCount; i++)
                    {
                        int num = ((clsSceneBase) clsScene).get_CharIDByIndex(i);
                        if (!clsCollection.Contains(Conversions.ToString(num)))
                        {
                            clsCollection.Add(num, Conversions.ToString(num));
                        }
                    }

                    int itemCount = clsScene.ItemCount;
                    for (int j = 1; j <= itemCount; j++)
                    {
                        int num2 = ((clsSceneBase) clsScene).get_ItemIDByIndex(j);
                        if (!clsCollection2.Contains(Conversions.ToString(num2)))
                        {
                            clsCollection2.Add(num2, Conversions.ToString(num2));
                        }
                    }

                    int locationCount = clsScene.LocationCount;
                    for (int k = 1; k <= locationCount; k++)
                    {
                        int num3 = ((clsSceneBase) clsScene).get_LocationIDByIndex(k);
                        if (!clsCollection3.Contains(Conversions.ToString(num3)))
                        {
                            clsCollection3.Add(num3, Conversions.ToString(num3));
                        }
                    }

                    stringBuilder.AppendLine(text);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            stringBuilder.AppendLine("</SCENES>");
            stringBuilder2.AppendLine(PackageLocations(clsCollection3));
            stringBuilder2.AppendLine(PackageItems(clsCollection2));
            stringBuilder2.AppendLine(PackageChars(clsCollection));
            stringBuilder2.Append(stringBuilder.ToString());
            return stringBuilder2.ToString();
        }

        public string PackageLocations(clsCollection<int> colLocIDs)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("<LOCATIONS>");
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = colLocIDs.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    int iD = Conversions.ToInteger(enumerator.Current);
                    stringBuilder.AppendLine(this.get_LocationByID(iD).DataAsXML());
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            stringBuilder.AppendLine("</LOCATIONS>");
            return stringBuilder.ToString();
        }

        public string PackageItems(clsCollection<int> colItemIDs)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("<ITEMS>");
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = colItemIDs.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    int iD = Conversions.ToInteger(enumerator.Current);
                    stringBuilder.AppendLine(this.get_ItemByID(iD).DataAsXML());
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            stringBuilder.AppendLine("</ITEMS>");
            return stringBuilder.ToString();
        }

        public string PackageChars(clsCollection<int> colCharIDs)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("<CHARACTERS>");
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = colCharIDs.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    int iD = Conversions.ToInteger(enumerator.Current);
                    stringBuilder.AppendLine(this.get_CharByID(iD).DataAsXML());
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            stringBuilder.AppendLine("</CHARACTERS>");
            return stringBuilder.ToString();
        }

        public void PasteChapters(string strData, bool Merge)
        {
            clsCollection<clsPair> colChanges = new clsCollection<clsPair>();
            PasteItems(strData, ref colChanges, Merge);
            clsCollection<clsPair> colChanges2 = new clsCollection<clsPair>();
            PasteLocs(strData, ref colChanges2, Merge);
            clsCollection<clsPair> colChanges3 = new clsCollection<clsPair>();
            PasteChars(strData, ref colChanges3, Merge);
            clsScenes clsScenes = new clsScenes();
            clsCollection<int> clsCollection = new clsCollection<int>();
            if (clsScenes.CreateFromXML(ProjectPath(), strData, RTFIncluded: true, TriggerSaveEvent: false, V6File))
            {
                clsChapters clsChapters = new clsChapters();
                if (clsChapters.CreateFromXML(strData))
                {
                    IEnumerator enumerator = default(IEnumerator);
                    try
                    {
                        enumerator = clsChapters.GetEnumerator();
                        IEnumerator enumerator2 = default(IEnumerator);
                        while (enumerator.MoveNext())
                        {
                            clsChapter cChapter = (clsChapter) enumerator.Current;
                            cChapter.ID = 0;
                            AddChapter(ref cChapter);
                            clsCollection.Clear();
                            int sceneCount = cChapter.SceneCount;
                            for (int i = 1; i <= sceneCount; i++)
                            {
                                int item = cChapter.get_SceneIDByIndex(i);
                                clsCollection.Add(item);
                            }

                            cChapter.RemoveAllScenes();
                            try
                            {
                                enumerator2 = clsCollection.GetEnumerator();
                                while (enumerator2.MoveNext())
                                {
                                    int num = Conversions.ToInteger(enumerator2.Current);
                                    if (clsScenes.Contains(num))
                                    {
                                        clsScene Scene = clsScenes.SceneByID(num);
                                        Scene.BelongsToChID = cChapter.ID;
                                        Scene.ID = 0;
                                        cScenes.AddScene(ref Scene);
                                        UpdateObjectIDs(ref Scene, colChanges2, colChanges, colChanges3);
                                        Scene.ClearRTFFilename();
                                        Scene.SaveRTFV5(ProjectPath());
                                        int iD = Scene.ID;
                                        cChapter.AddSceneID(iD);
                                    }
                                    else
                                    {
                                        clsShared.cLog.AddAction(
                                            "Scene ID " + Conversions.ToString(num) +
                                            " not found during chapter paste");
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 is IDisposable)
                                {
                                    (enumerator2 as IDisposable).Dispose();
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator is IDisposable)
                        {
                            (enumerator as IDisposable).Dispose();
                        }
                    }
                }
            }
        }

        public void PasteScenes(int ChID, string strData, bool Merge)
        {
            clsCollection<clsPair> colChanges = new clsCollection<clsPair>();
            PasteItems(strData, ref colChanges, Merge);
            clsCollection<clsPair> colChanges2 = new clsCollection<clsPair>();
            PasteLocs(strData, ref colChanges2, Merge);
            clsCollection<clsPair> colChanges3 = new clsCollection<clsPair>();
            PasteChars(strData, ref colChanges3, Merge);
            clsScenes clsScenes = new clsScenes();
            if (clsScenes.CreateFromXML(ProjectPath(), strData, RTFIncluded: true, TriggerSaveEvent: false, V6File))
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = clsScenes.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsScene Sc = (clsScene) enumerator.Current;
                        UpdateObjectIDs(ref Sc, colChanges2, colChanges, colChanges3);
                        Sc.ID = 0;
                        AddSceneToProjectAndChapter(ref Sc, ChID);
                        Sc.ClearRTFFilename();
                        Sc.SaveRTFV5(ProjectPath());
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
        }

        private void UpdateObjectIDs(ref clsScene Sc, clsCollection<clsPair> colNewLocIDs,
            clsCollection<clsPair> colNewItemIDs, clsCollection<clsPair> colNewCharIDs)
        {
            clsCollection<int> clsCollection = new clsCollection<int>();
            clsCollection.Clear();
            int itemCount = Sc.ItemCount;
            for (int i = 1; i <= itemCount; i++)
            {
                int value = ((clsSceneBase) Sc).get_ItemIDByIndex(i);
                clsCollection.Add(colNewItemIDs[Conversions.ToString(value)].NewID);
            }

            Sc.RemoveAllItems();
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = clsCollection.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    int iD = Conversions.ToInteger(enumerator.Current);
                    Sc.AddItemByID(iD);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            clsCollection.Clear();
            int charCount = Sc.CharCount;
            for (int j = 1; j <= charCount; j++)
            {
                int value = ((clsSceneBase) Sc).get_CharIDByIndex(j);
                clsCollection.Add(colNewCharIDs[Conversions.ToString(value)].NewID);
            }

            Sc.RemoveAllChar();
            IEnumerator enumerator2 = default(IEnumerator);
            try
            {
                enumerator2 = clsCollection.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    int iD2 = Conversions.ToInteger(enumerator2.Current);
                    Sc.AddCharByID(iD2);
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }

            clsCollection.Clear();
            int locationCount = Sc.LocationCount;
            for (int k = 1; k <= locationCount; k++)
            {
                int value = ((clsSceneBase) Sc).get_LocationIDByIndex(k);
                clsCollection.Add(colNewLocIDs[Conversions.ToString(value)].NewID);
            }

            Sc.RemoveAllLoc();
            IEnumerator enumerator3 = default(IEnumerator);
            try
            {
                enumerator3 = clsCollection.GetEnumerator();
                while (enumerator3.MoveNext())
                {
                    int iD3 = Conversions.ToInteger(enumerator3.Current);
                    Sc.AddLocationByID(iD3);
                }
            }
            finally
            {
                if (enumerator3 is IDisposable)
                {
                    (enumerator3 as IDisposable).Dispose();
                }
            }
        }

        private void PasteItems(string strData, ref clsCollection<clsPair> colChanges, bool Merge)
        {
            clsItems clsItems = new clsItems();
            if (clsItems.CreateFromXML(strData))
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = clsItems.GetEnumerator();
                    IEnumerator enumerator2 = default(IEnumerator);
                    while (enumerator.MoveNext())
                    {
                        clsItem Item = (clsItem) enumerator.Current;
                        string title = Item.Title;
                        int num = -1;
                        if (Merge)
                        {
                            try
                            {
                                enumerator2 = Items.GetEnumerator();
                                while (enumerator2.MoveNext())
                                {
                                    clsItem clsItem = (clsItem) enumerator2.Current;
                                    if (clsShared.cText.StringsEqual(clsItem.Title, title))
                                    {
                                        num = clsItem.ID;
                                        break;
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 is IDisposable)
                                {
                                    (enumerator2 as IDisposable).Dispose();
                                }
                            }
                        }

                        clsPair clsPair = new clsPair();
                        clsPair.OldID = Item.ID;
                        if (num == -1)
                        {
                            Item.ID = 0;
                            AddItem(ref Item);
                            clsPair.NewID = Item.ID;
                        }
                        else
                        {
                            clsPair.NewID = num;
                        }

                        colChanges.Add(clsPair, Conversions.ToString(clsPair.OldID));
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
        }

        private void PasteChars(string strData, ref clsCollection<clsPair> colChanges, bool Merge)
        {
            clsCharacters clsCharacters = new clsCharacters();
            if (clsCharacters.CreateFromXML(strData))
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = clsCharacters.GetEnumerator();
                    IEnumerator enumerator2 = default(IEnumerator);
                    while (enumerator.MoveNext())
                    {
                        clsCharacter cChr = (clsCharacter) enumerator.Current;
                        string title = cChr.Title;
                        int num = -1;
                        if (Merge)
                        {
                            try
                            {
                                enumerator2 = Chars.GetEnumerator();
                                while (enumerator2.MoveNext())
                                {
                                    clsCharacter clsCharacter = (clsCharacter) enumerator2.Current;
                                    if (clsShared.cText.StringsEqual(clsCharacter.Title, title))
                                    {
                                        num = clsCharacter.ID;
                                        break;
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 is IDisposable)
                                {
                                    (enumerator2 as IDisposable).Dispose();
                                }
                            }
                        }

                        clsPair clsPair = new clsPair();
                        clsPair.OldID = cChr.ID;
                        if (num == -1)
                        {
                            cChr.ID = 0;
                            AddChar(ref cChr);
                            clsPair.NewID = cChr.ID;
                        }
                        else
                        {
                            clsPair.NewID = num;
                        }

                        colChanges.Add(clsPair, Conversions.ToString(clsPair.OldID));
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
        }

        private void PasteLocs(string strData, ref clsCollection<clsPair> colChanges, bool Merge)
        {
            clsLocations clsLocations = new clsLocations();
            if (clsLocations.CreateFromXML(strData))
            {
                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = clsLocations.GetEnumerator();
                    IEnumerator enumerator2 = default(IEnumerator);
                    while (enumerator.MoveNext())
                    {
                        clsLocation Location = (clsLocation) enumerator.Current;
                        string title = Location.Title;
                        int num = -1;
                        if (Merge)
                        {
                            try
                            {
                                enumerator2 = Locations.GetEnumerator();
                                while (enumerator2.MoveNext())
                                {
                                    clsLocation clsLocation = (clsLocation) enumerator2.Current;
                                    if (clsShared.cText.StringsEqual(clsLocation.Title, title))
                                    {
                                        num = clsLocation.ID;
                                        break;
                                    }
                                }
                            }
                            finally
                            {
                                if (enumerator2 is IDisposable)
                                {
                                    (enumerator2 as IDisposable).Dispose();
                                }
                            }
                        }

                        clsPair clsPair = new clsPair();
                        clsPair.OldID = Location.ID;
                        if (num == -1)
                        {
                            Location.ID = 0;
                            AddLocation(ref Location);
                            clsPair.NewID = Location.ID;
                        }
                        else
                        {
                            clsPair.NewID = num;
                        }

                        colChanges.Add(clsPair, Conversions.ToString(clsPair.OldID));
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }
            }
        }

        public clsCollection<int> ReadCustomExport(string ExportTitle)
        {
            int num = cExports.IDByTitle(ExportTitle);
            if (num > 0)
            {
                clsExport clsExport = ((clsGenericBase<clsExport>) cExports).get_ByID(num);
                clsCollection<string> clsCollection = new clsCollection<string>();
                clsCollection<int> clsCollection2 = new clsCollection<int>();
                string[] aKAArray = clsExport.AKAArray;
                foreach (string text in aKAArray)
                {
                    if (text.Length > 0)
                    {
                        clsCollection.Add(text, text);
                    }
                }

                IEnumerator enumerator = default(IEnumerator);
                try
                {
                    enumerator = Chapters.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        clsChapter clsChapter = (clsChapter) enumerator.Current;
                        if (clsCollection.Contains(clsChapter.ID))
                        {
                            clsCollection2.Add(clsChapter.ID, Conversions.ToString(clsChapter.ID));
                        }
                    }
                }
                finally
                {
                    if (enumerator is IDisposable)
                    {
                        (enumerator as IDisposable).Dispose();
                    }
                }

                return clsCollection2;
            }

            return new clsCollection<int>();
        }

        public string ReadCustomExportFilename(string ExportTitle)
        {
            int num = cExports.IDByTitle(ExportTitle);
            if (num > 0)
            {
                return ((clsGenericBase<clsExport>) cExports).get_ByID(num).Filename;
            }

            return "";
        }

        public void StoreCustomExport(string ExportTitle, clsCollection<int> colChapterIDs, string ExportFilename,
            string EpubExporterName, bool DoMobi, bool DoEpub)
        {
            DeleteCustomExport(ExportTitle);
            clsExport cObject = new clsExport();
            string text = "";
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = colChapterIDs.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    string text2 = Conversions.ToString(enumerator.Current);
                    if (text2.Length > 0)
                    {
                        text = text + text2 + ";";
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            cObject.Title = ExportTitle;
            cObject.ChIDsSemiColonSeparated = text;
            cObject.Filename = ExportFilename;
            cObject.ImageFile = EpubExporterName;
            if (DoMobi && DoEpub)
            {
                cObject.Tags = "mobi;epub";
            }
            else if (DoMobi)
            {
                cObject.Tags = "mobi";
            }
            else if (DoEpub)
            {
                cObject.Tags = "epub";
            }
            else
            {
                cObject.Tags = "";
            }

            cExports.Added(ref cObject);
            QuickSave();
        }

        public bool DeleteCustomExport(string ExportTitle)
        {
            int num = cExports.IDByTitle(ExportTitle);
            if (num > 0)
            {
                cExports.RemoveByID(num);
            }

            return true;
        }

        public void ProgressHandler(string strProgress)
        {
            ProgressEvent?.Invoke(strProgress);
        }

        public void UnlockProject(string Filename)
        {
            cProjectIO.UnlockProject(Filename);
        }

        public bool ExportCharacters(string Filename)
        {
            //Discarded unreachable code: IL_00c5, IL_00ca, IL_00e1, IL_00ec, IL_00fc, IL_0166, IL_0168, IL_017c, IL_019e
            int num = default(int);
            bool flag=false;
            int num3 = default(int);
            try
            {
                ProjectData.ClearProjectError();
                num = 2;
                int num2 = 2;
                flag = true;
                num2 = 3;
                StringBuilder SBP = new StringBuilder();
                num2 = 4;
                if (V6File)
                {
                    num2 = 5;
                    SBP.AppendLine(cXml.XMLHeaderV6);
                }
                else
                {
                    num2 = 7;
                    SBP.AppendLine(modSubs.XMLHeaderV5);
                }

                num2 = 8;
                if (!cChars.WriteXML(ref SBP))
                {
                    num2 = 9;
                    flag = false;
                }
                else
                {
                    num2 = 11;
                    if (V6File)
                    {
                        num2 = 12;
                        flag = clsShared.cFile.WriteUnicodeForced(Filename, SBP.ToString());
                    }
                    else
                    {
                        num2 = 14;
                        flag = clsShared.cFile.WriteFileSysEncoded(Filename, SBP.ToString());
                    }
                }

                num2 = 15;
                if (flag)
                {
                    num2 = 16;
                    Interaction.MsgBox("Your data was saved to " + Filename);
                }
                else
                {
                    num2 = 18;
                    Interaction.MsgBox("Unable to save file.");
                }
            }
            catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
            {
                ProjectData.SetProjectError((Exception) obj);
                /*Error near IL_019c: Could not find block for branch target IL_0168*/
                ;
            }

            if (num3 != 0)
            {
                ProjectData.ClearProjectError();
            }

            return flag;
        }

        public bool ExportItems(string Filename)
        {
            //Discarded unreachable code: IL_00c5, IL_00ca, IL_00e1, IL_00ec, IL_00fc, IL_0166, IL_0168, IL_017c, IL_019e
            int num = default(int);
            bool flag = false;
            int num3 = default(int);
            try
            {
                ProjectData.ClearProjectError();
                num = 2;
                int num2 = 2;
                flag = true;
                num2 = 3;
                StringBuilder SBP = new StringBuilder();
                num2 = 4;
                if (V6File)
                {
                    num2 = 5;
                    SBP.AppendLine(cXml.XMLHeaderV6);
                }
                else
                {
                    num2 = 7;
                    SBP.AppendLine(modSubs.XMLHeaderV5);
                }

                num2 = 8;
                if (!cItems.WriteXML(ref SBP))
                {
                    num2 = 9;
                    flag = false;
                }
                else
                {
                    num2 = 11;
                    if (V6File)
                    {
                        num2 = 12;
                        flag = clsShared.cFile.WriteUnicodeForced(Filename, SBP.ToString());
                    }
                    else
                    {
                        num2 = 14;
                        flag = clsShared.cFile.WriteFileSysEncoded(Filename, SBP.ToString());
                    }
                }

                num2 = 15;
                if (flag)
                {
                    num2 = 16;
                    Interaction.MsgBox("Your data was saved to " + Filename);
                }
                else
                {
                    num2 = 18;
                    Interaction.MsgBox("Unable to save file.");
                }
            }
            catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
            {
                ProjectData.SetProjectError((Exception) obj);
                /*Error near IL_019c: Could not find block for branch target IL_0168*/
                ;
            }

            if (num3 != 0)
            {
                ProjectData.ClearProjectError();
            }

            return flag;
        }

        public bool ExportLocations(string Filename)
        {
            //Discarded unreachable code: IL_00c5, IL_00ca, IL_00e1, IL_00ec, IL_00fc, IL_0166, IL_0168, IL_017c, IL_019e
            int num = default(int);
            bool flag = false;
            int num3 = default(int);
            try
            {
                ProjectData.ClearProjectError();
                num = 2;
                int num2 = 2;
                flag = true;
                num2 = 3;
                StringBuilder SBP = new StringBuilder();
                num2 = 4;
                if (V6File)
                {
                    num2 = 5;
                    SBP.AppendLine(cXml.XMLHeaderV6);
                }
                else
                {
                    num2 = 7;
                    SBP.AppendLine(modSubs.XMLHeaderV5);
                }

                num2 = 8;
                if (!cLocations.WriteXML(ref SBP))
                {
                    num2 = 9;
                    flag = false;
                }
                else
                {
                    num2 = 11;
                    if (V6File)
                    {
                        num2 = 12;
                        flag = clsShared.cFile.WriteUnicodeForced(Filename, SBP.ToString());
                    }
                    else
                    {
                        num2 = 14;
                        flag = clsShared.cFile.WriteFileSysEncoded(Filename, SBP.ToString());
                    }
                }

                num2 = 15;
                if (flag)
                {
                    num2 = 16;
                    Interaction.MsgBox("Your data was saved to " + Filename);
                }
                else
                {
                    num2 = 18;
                    Interaction.MsgBox("Unable to save file.");
                }
            }
            catch (Exception obj) when ((obj is Exception && num != 0) & (num3 == 0))
            {
                ProjectData.SetProjectError((Exception) obj);
                /*Error near IL_019c: Could not find block for branch target IL_0168*/
                ;
            }

            if (num3 != 0)
            {
                ProjectData.ClearProjectError();
            }

            return flag;
        }

        public bool ImportCharacters(string Filename)
        {
            //Discarded unreachable code: IL_0090, IL_0095, IL_00ac, IL_00b7, IL_00c7, IL_010d, IL_010f, IL_0123, IL_0145
            int num = default(int);
            bool result=false;
            int num4 = default(int);
            try
            {
                ProjectData.ClearProjectError();
                num = 2;
                int num2 = 2;
                int count = cChars.Count;
                num2 = 3;
                result = true;
                num2 = 4;
                string TextString = "";
                num2 = 5;
                clsShared.cFile.ReadTextAuto(Filename, ref TextString, UseSysEncoding: true);
                num2 = 6;
                cChars.UpdateFromXML(TextString, ResetID: true, TriggerSaveEvent: true);
                num2 = 7;
                int num3 = count + 1;
                int count2 = cChars.Count;
                for (int i = num3; i <= count2; i++)
                {
                    num2 = 8;
                    AddedNewCharEvent?.Invoke(cChars.IDFromIndex(i));
                    num2 = 9;
                }
            }
            catch (Exception obj) when ((obj is Exception && num != 0) & (num4 == 0))
            {
                ProjectData.SetProjectError((Exception) obj);
                /*Error near IL_0143: Could not find block for branch target IL_010f*/
                ;
            }

            if (num4 != 0)
            {
                ProjectData.ClearProjectError();
            }

            return result;
        }

        public bool ImportLocations(string Filename)
        {
            //Discarded unreachable code: IL_0090, IL_0095, IL_00ac, IL_00b7, IL_00c7, IL_010d, IL_010f, IL_0123, IL_0145
            int num = default(int);
            bool result = false;
            int num4 = default(int);
            try
            {
                ProjectData.ClearProjectError();
                num = 2;
                int num2 = 2;
                int count = cLocations.Count;
                num2 = 3;
                result = true;
                num2 = 4;
                string TextString = "";
                num2 = 5;
                clsShared.cFile.ReadTextAuto(Filename, ref TextString, UseSysEncoding: true);
                num2 = 6;
                cLocations.UpdateFromXML(TextString, ResetID: true, TriggerSaveEvent: true);
                num2 = 7;
                int num3 = count + 1;
                int count2 = cLocations.Count;
                for (int i = num3; i <= count2; i++)
                {
                    num2 = 8;
                    AddedNewLocationEvent?.Invoke(cLocations.IDFromIndex(i));
                    num2 = 9;
                }
            }
            catch (Exception obj) when ((obj is Exception && num != 0) & (num4 == 0))
            {
                ProjectData.SetProjectError((Exception) obj);
                /*Error near IL_0143: Could not find block for branch target IL_010f*/
                ;
            }

            if (num4 != 0)
            {
                ProjectData.ClearProjectError();
            }

            return result;
        }

        public bool ImportItems(string Filename)
        {
            //Discarded unreachable code: IL_0090, IL_0095, IL_00ac, IL_00b7, IL_00c7, IL_010d, IL_010f, IL_0123, IL_0145
            int num = default(int);
            bool result = false;
            int num4 = default(int);
            try
            {
                ProjectData.ClearProjectError();
                num = 2;
                int num2 = 2;
                int count = cItems.Count;
                num2 = 3;
                result = true;
                num2 = 4;
                string TextString = "";
                num2 = 5;
                clsShared.cFile.ReadTextAuto(Filename, ref TextString, UseSysEncoding: true);
                num2 = 6;
                cItems.UpdateFromXML(TextString, ResetID: true, TriggerSaveEvent: true);
                num2 = 7;
                int num3 = count + 1;
                int count2 = cItems.Count;
                for (int i = num3; i <= count2; i++)
                {
                    num2 = 8;
                    AddedNewItemEvent?.Invoke(cItems.IDFromIndex(i));
                    num2 = 9;
                }
            }
            catch (Exception obj) when ((obj is Exception && num != 0) & (num4 == 0))
            {
                ProjectData.SetProjectError((Exception) obj);
                /*Error near IL_0143: Could not find block for branch target IL_010f*/
                ;
            }

            if (num4 != 0)
            {
                ProjectData.ClearProjectError();
            }

            return result;
        }

        public string ExportAsText(bool SaveSummaries, bool SaveUnused)
        {
            StringBuilder stringBuilder = new StringBuilder();
            IEnumerator enumerator = default(IEnumerator);
            try
            {
                enumerator = Chapters.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    clsChapter clsChapter = (clsChapter) enumerator.Current;
                    if (!clsChapter.Unused | (clsChapter.Type == ChapterTypes.ChapterNormal) | SaveUnused)
                    {
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("[PB]");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("<center>*" + clsChapter.Title + "*");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("");
                        int sceneCount = clsChapter.SceneCount;
                        for (int i = 1; i <= sceneCount; i++)
                        {
                            int iD = clsChapter.get_SceneIDByIndex(i);
                            clsScene clsScene = this.get_SceneByID(iD);
                            if (!(clsScene.ExportCondSpecific & !clsScene.ExportWhenText) &&
                                (SaveUnused | !clsScene.Unused))
                            {
                                if (SaveSummaries)
                                {
                                    stringBuilder.AppendLine(clsScene.Title);
                                    stringBuilder.AppendLine("");
                                    if (SaveSummaries)
                                    {
                                        stringBuilder.AppendLine(clsScene.DescText);
                                        stringBuilder.AppendLine("");
                                        stringBuilder.AppendLine("");
                                    }
                                }

                                stringBuilder.AppendLine(clsScene.ConvertedToPlainText());
                                stringBuilder.AppendLine("");
                                if (i < clsChapter.SceneCount && !this.get_SceneByChID(clsChapter.ID, i + 1).Unused)
                                {
                                    stringBuilder.AppendLine("<center>* * *");
                                    stringBuilder.AppendLine("");
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }

            return stringBuilder.ToString();
        }

        private void SaveTimer_Tick(object sender, EventArgs e)
        {
            if (colSaveTokens.Count > 0)
            {
                clsShared.cLog.AddAction("Triggering auto save. Token count: " +
                                         Conversions.ToString(colSaveTokens.Count));
                ProgressEvent?.Invoke("Autosaving project");
                if (!Save(CriticalSave: true))
                {
                    ProgressEvent?.Invoke("Autosave failed");
                    clsShared.cLog.AddAction("Auto save failed, keeping save tokens");
                }
            }
        }
    }
}